pandas 如何从熊猫数据框中提取单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32208604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract cell from pandas data frame
提问by bill999
Say I make a pandasdataframe (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 val1as a string, not as a pandasobject.
我想val1作为字符串访问,而不是作为pandas对象访问。
If I do:
如果我做:
cell = data.iloc[[0],[0]]
type(cell)
I see that cellis 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 val1as a strobject and not a pandasobject?
我怎样才能得到val1一个str对象而不是一个pandas对象?
回答by Anand S Kumar
The issue for your case is that you are passing lists to iloc, you need to pass normal integers 0,0to ilocto get the result you want -
您的情况的问题是您将列表传递给iloc,您需要将普通整数传递0,0给iloc以获得您想要的结果 -
In [85]: data.iloc[0,0]
Out[85]: 'val1'
回答by Tom Patel
In order to access the value within the DataFrame as a string:
为了以字符串形式访问 DataFrame 中的值:
cell = str(data.iloc[0,0])

