如何在 Pandas 中获取 DataFrame 的特定列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38467470/
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 get particular Column of DataFrame in pandas?
提问by Pankaj Mishra
I have a data frame name it dfI want to have like its first and second colums(series) in variable xand y.
我有一个数据框名称df我想在变量x和y 中拥有它的第一和第二列(系列)。
I would have done that by name of the column like df['A']
or df['B']
or something like that.
我想这样做,是由像列名df['A']
或df['B']
或类似的东西。
But problem here is that data is itself header and it has no name.Header is like 2.17 ,3.145 like that.
但这里的问题是数据本身就是标题,它没有名称。标题就像 2.17 ,3.145 那样。
So my Question is:
所以我的问题是:
a) How to name column and start the data(which starts now from head) right after the name ?
a)如何命名列并在名称之后立即开始数据(现在从头开始)?
b) How to get particular column's data if we don't know the name or it doesn't have the name ?
b) 如果我们不知道名称或没有名称,如何获取特定列的数据?
Thank you.
谢谢你。
回答by Ami Tavory
You might want to read the documentation on indexing.
您可能想阅读有关 indexing的 文档。
For what you specified in the question, you can use
对于您在问题中指定的内容,您可以使用
x, y = df.iloc[:, [0]], df.iloc[:, [1]]
回答by Alex
Set the names
kwarg when reading the DataFrame (see the read_csv
docs.
names
在读取 DataFrame 时设置kwarg(请参阅read_csv
docs.
So instead of pd.read_csv('kndkma')
use pd.read_csv('kndkma', names=['a', 'b', ...])
.
所以,而不是pd.read_csv('kndkma')
使用pd.read_csv('kndkma', names=['a', 'b', ...])
.
回答by chapelo
It is usually easier to name the columns when you read or create the DataFrame, but you can also name (or rename) the columns afterwards with something like:
在读取或创建 DataFrame 时命名列通常更容易,但您也可以在之后命名(或重命名)列,例如:
df.columns = ['A','B', ...]