[英]How to extract cell from pandas data frame
Say I make a pandas
dataframe (I am not good at pandas
, and this may not be very efficient): 假设我制作了一个
pandas
数据帧(我不擅长pandas
,这可能效率不高):
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. 我想将
val1
作为字符串访问,而不是作为pandas
对象访问。
If I do: 如果我做:
cell = data.iloc[[0],[0]]
type(cell)
I see that cell
is of type <class 'pandas.core.frame.DataFrame'>
我看到该
cell
类型为<class 'pandas.core.frame.DataFrame'>
If I then do: 如果我那么做:
type(cell[`a'])
I see that it is <class 'pandas.core.series.Series'>
我看到它是
<class 'pandas.core.series.Series'>
How can I get val1
as a str
object and not a pandas
object? 如何将
val1
作为str
对象而不是pandas
对象?
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 - 您的案例的问题是您将列表传递给
iloc
,您需要将正常的整数0,0
传递给iloc
以获得您想要的结果 -
In [85]: data.iloc[0,0]
Out[85]: 'val1'
要以字符串形式访问DataFrame中的值:
cell = str(data.iloc[0,0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.