6

How do I convert a pandas data frame with 1 row, 1 column into a scalar value?

import pandas as pd
data = {'col1': 1}
df = pd.DataFrame(data=d)
runningbirds
  • 6,235
  • 13
  • 55
  • 94

2 Answers2

5

You can use iat to access scalar elements specifying the location by integer (i.e. 0,0 for the top left element, as opposed to at which would take the row and columns labels rather than integer index).

So if you only have one element in your dataframe, df.iat[0,0] will get it for you.

In [139]: import pandas as pd
In [140]: df = pd.DataFrame(data=[1], columns=['col1'])
In [141]: df.iat[0,0]
Out[141]: 1
In [143]: df
Out[143]: 
   col1
0     1
LangeHaare
  • 2,776
  • 2
  • 17
  • 25
2

You can use squeeze.

import pandas as pd
df = pd.DataFrame(data=[1], columns=['col1'])
df.squeeze()
deallen7
  • 21
  • 1