How to get a value from a cell of a dataframe? How to get a value from a cell of a dataframe? python python

How to get a value from a cell of a dataframe?


If you have a DataFrame with only one row, then access the first (only) row as a Series using iloc, and then the value using the column name:

In [3]: sub_dfOut[3]:          A         B2 -0.133653 -0.030854In [4]: sub_df.iloc[0]Out[4]:A   -0.133653B   -0.030854Name: 2, dtype: float64In [5]: sub_df.iloc[0]['A']Out[5]: -0.13365288513107493


These are fast access for scalars

In [15]: df = pandas.DataFrame(numpy.random.randn(5,3),columns=list('ABC'))In [16]: dfOut[16]:           A         B         C0 -0.074172 -0.090626  0.0382721 -0.128545  0.762088 -0.7148162  0.201498 -0.734963  0.5583973  1.563307 -1.186415  0.8482464  0.205171  0.962514  0.037709In [17]: df.iat[0,0]Out[17]: -0.074171888537611502In [18]: df.at[0,'A']Out[18]: -0.074171888537611502


You can turn your 1x1 dataframe into a numpy array, then access the first and only value of that array:

val = d2['col_name'].values[0]


matomo