I'm trying to do something in Python that I can do easily in Excel, but it's simply not working out. Essentially, I want to take a dataframe, look at 2 columns in the dataframe and create a third column based on conditionals. In short, I want to look at foo, determine using a conditional what category it would fall into, and then add that result to thud for a new column with a value of conditional foo + thud.
My simplistic approach was to iterate over the dataframe, use a dictionary to store my key:values for the conditional test of foo and then create a new column with the result of the foo conditional plus thud.
My challenge is every solution I've tried, I end up with this error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
My algorithm is sound I feel but the coding execution using Pandas isn't working the way I thought it would.
How can I avoid the Value errors? Or am I completely offbase in my approach? Thank you for your time!
without your code its impossible to tell... but based on the error you described i would expect this to solve your issue
you cannot do if dataframe
you can do if dataframe is None:
if you want to test if its none
or if len(dataframe) == 0:
if you want to test ifits empty
however more to the point, usually you use pandas to avoid looping
I am going to go out on a limb and assume you want something like the following
coin_values = {'Gold':100,'Silver':10,'Bronze':1}
my_coin_values = df['Thud'].map(coin_values)
total_moneys = my_coin_values * df['foo']
print(df['qux'][total_moneys > 500])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.