I have a pandas dataFrame which consists of many columns but just one row. When I iterate through each column to get the contain of each row, I get 2 separate numbers; namely one for the index and the number itself. Assuming that df is my dataFrame, I do the following
for k,v in df.iteritems():
if df[k]>df.mean(axis=1):
print(" do something")
I get the following error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
When I print df[k], I have two values 0 num
How can I drop the first value?
Edit:
This is the structure of my dataframe
column1 column2 column3
0 2 3 -4
I now have
for k,v in df.iteritems():
if df[k][0]>3* abs(df.mean(axis=1)):
print(" do something")
and the error is now :TypeError: len() of unsized object
You get a Series
returned, to get just the scalar value then just do:
df[k].values[0]
Example:
In [190]:
df = pd.DataFrame(columns=list('abc'), data = np.random.randn(1,3))
df
Out[190]:
a b c
0 0.994306 -0.340043 -0.551422
In [191]:
for col in df:
print(df[col].values[0])
0.994306161647
-0.340042912281
-0.551421752498
Also this would work too if you hate typing:
df[k][0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With