Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the contents of a pandas DateFrame single row without its index

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

like image 650
user3841581 Avatar asked Dec 24 '22 12:12

user3841581


1 Answers

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]
like image 88
EdChum Avatar answered Apr 08 '23 06:04

EdChum