Let's say a have a .csv file like this:
Symbol,Address,Website,Price,Vol
URBT,,,0.99,12345
TSPG,,,1.99,12346
CRBO,,,2.99,12347
PVSP,,,3.99,12348
TPRP,,,4.99,12349
VMHG,,,5.99,12350
TORM,,,6.99,12351
SORT,,,7.99,12352
MRTI,,,8.99,12353
VTMC,,,9.99,12354
I want to update the Address and Website columns with this:
[{'ticker': 'VMHG', 'address': '555 NE 34th St. Suite 1207', 'website': 'http://www.VictoryYachts.com'}, {'ticker': 'CRBO', 'address': '1700 Broadway', 'website': 'http://www.carbonenergycorp.com'}, {'ticker': 'PVSP', 'address': '800 Westchester Ave.', 'website': 'https://www.pervasip.net'}, {'ticker': 'VTMC', 'address': '55 W. 47 Street', 'website': 'http://www.vtmc.us'}, {'ticker': 'SORT', 'address': '31 Clinton Ave', 'website': 'http://www.incjet.com'}, {'ticker': 'URBT', 'address': '11705 Willake Street', 'website': 'https://urbt.tv'}, {'ticker': 'TORM', 'address': '722 Burleson Street', 'website': 'http://www.torminerals.com'}, {'ticker': 'MRTI', 'address': '104 Armour Road', 'website': 'http://www.mrti.com'}, {'ticker': 'TPRP', 'address': '1000 Walnut St.', 'website': 'http://www.towerproperties.com'}, {'ticker': 'TSPG', 'address': '525 Milltown Rd,', 'website': 'http://www.tgipower.com/'}]
I can do this like so:
import pandas as pd
if __name__ == "__main__":
df = pd.read_csv("tickers.csv")
symbols = df["Symbol"].to_list()
scraped_data = [{'ticker': 'VMHG', 'address': '555 NE 34th St. Suite 1207', 'website': 'http://www.VictoryYachts.com'}, {'ticker': 'CRBO', 'address': '1700 Broadway', 'website': 'http://www.carbonenergycorp.com'}, {'ticker': 'PVSP', 'address': '800 Westchester Ave.', 'website': 'https://www.pervasip.net'}, {'ticker': 'VTMC', 'address': '55 W. 47 Street', 'website': 'http://www.vtmc.us'}, {'ticker': 'SORT', 'address': '31 Clinton Ave', 'website': 'http://www.incjet.com'}, {'ticker': 'URBT', 'address': '11705 Willake Street', 'website': 'https://urbt.tv'}, {'ticker': 'TORM', 'address': '722 Burleson Street', 'website': 'http://www.torminerals.com'}, {'ticker': 'MRTI', 'address': '104 Armour Road', 'website': 'http://www.mrti.com'}, {'ticker': 'TPRP', 'address': '1000 Walnut St.', 'website': 'http://www.towerproperties.com'}, {'ticker': 'TSPG', 'address': '525 Milltown Rd,', 'website': 'http://www.tgipower.com/'}]
sorted_scraped_data = sorted(
scraped_data, key=lambda i: symbols.index(i["ticker"])
)
df.loc[:, ["Address", "Website"]] = [
[i["address"], i["website"]] for i in sorted_scraped_data
]
df.to_csv("tickers.csv", index=False)
This works, but feels like a hacky workaround to match the order of the Symbol column with the list of dicts.
If I want to do this with pure pandas, without sorting first, for example:
import pandas as pd
if __name__ == "__main__":
df = pd.read_csv("tickers.csv")
symbols = df["Symbol"].to_list()
scraped_data = [{'ticker': 'VMHG', 'address': '555 NE 34th St. Suite 1207', 'website': 'http://www.VictoryYachts.com'}, {'ticker': 'CRBO', 'address': '1700 Broadway', 'website': 'http://www.carbonenergycorp.com'}, {'ticker': 'PVSP', 'address': '800 Westchester Ave.', 'website': 'https://www.pervasip.net'}, {'ticker': 'VTMC', 'address': '55 W. 47 Street', 'website': 'http://www.vtmc.us'}, {'ticker': 'SORT', 'address': '31 Clinton Ave', 'website': 'http://www.incjet.com'}, {'ticker': 'URBT', 'address': '11705 Willake Street', 'website': 'https://urbt.tv'}, {'ticker': 'TORM', 'address': '722 Burleson Street', 'website': 'http://www.torminerals.com'}, {'ticker': 'MRTI', 'address': '104 Armour Road', 'website': 'http://www.mrti.com'}, {'ticker': 'TPRP', 'address': '1000 Walnut St.', 'website': 'http://www.towerproperties.com'}, {'ticker': 'TSPG', 'address': '525 Milltown Rd,', 'website': 'http://www.tgipower.com/'}]
df.loc[:, ["Address", "Website"]] = [
[i["address"], i["website"]] for i in scraped_data
if df[(df["Symbol"] == i["ticker"])]
]
df.to_csv("tickers.csv", index=False)
I get a ValueError:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Question:
Why is that so? How can I update the df with the list of dicts to match the order of the Symbol column?
Desired output would be:
Symbol,Address,Website,Price,Vol
URBT,11705 Willake Street,https://urbt.tv,0.99,12345
TSPG,"525 Milltown Rd,",http://www.tgipower.com/,1.99,12346
CRBO,1700 Broadway,http://www.carbonenergycorp.com,2.99,12347
PVSP,800 Westchester Ave.,https://www.pervasip.net,3.99,12348
TPRP,1000 Walnut St.,http://www.towerproperties.com,4.99,12349
VMHG,555 NE 34th St. Suite 1207,http://www.VictoryYachts.com,5.99,12350
TORM,722 Burleson Street,http://www.torminerals.com,6.99,12351
SORT,31 Clinton Ave,http://www.incjet.com,7.99,12352
MRTI,104 Armour Road,http://www.mrti.com,8.99,12353
VTMC,55 W. 47 Street,http://www.vtmc.us,9.99,12354