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?
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"].
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.
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'
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