Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract cell from pandas data frame

Tags:

python

pandas

Say I make a pandas dataframe (I am not good at pandas, and this may not be very efficient):

import pandas as pd
colnames = ['a', 'b']
data = pd.DataFrame(columns=colnames)
df_row = ['val1', 'val2']
app = pd.Series(df_row, index=cols)
data = data.append(app, ignore_index=True)

I want to access val1 as a string, not as a pandas object.

If I do:

cell = data.iloc[[0],[0]]
type(cell)

I see that cell is of type <class 'pandas.core.frame.DataFrame'>

If I then do:

type(cell[`a'])

I see that it is <class 'pandas.core.series.Series'>

How can I get val1 as a str object and not a pandas object?

like image 858
bill999 Avatar asked Aug 25 '15 15:08

bill999


People also ask

How do I extract a cell from a DataFrame in Python?

Select Cell Value from DataFrame Using df['col_name']. values[] We can use df['col_name']. values[] to get 1×1 DataFrame as a NumPy array, then access the first and only value of that array to get a cell value, for instance, df["Duration"].

How do I extract values from a DataFrame column?

You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression. The blow example returns a Courses column where the Fee column value matches with 25000.


1 Answers

The issue for your case is that you are passing lists to iloc, you need to pass normal integers 0,0 to iloc to get the result you want -

In [85]: data.iloc[0,0]
Out[85]: 'val1'
like image 61
Anand S Kumar Avatar answered Oct 05 '22 21:10

Anand S Kumar