[英]set column of pandas.DataFrame object
Ideally, I want to be able something like: 理想情况下,我希望能够实现以下功能:
cols = ['A', 'B', 'C']
df = pandas.DataFrame(index=range(5), columns=cols)
df.get_column(cols[0]) = [1, 2, 3, 4, 5]
What is the pythonic/pandonic way to do this? 什么是pythonic / pandonic方法?
Edit: I know that I can access the column 'A' by df.A
, but in general I do not know what the column names are. 编辑:我知道我可以通过df.A
访问列“ A”,但通常我不知道列名是什么。
好的,这特别简单。
df[cols[0]] = [1, 2, 3, 4, 5]
You do not need to store what columns a DataFrame has separately. 您不需要存储DataFrame的哪些列。
You can find out what columns exist in a pandas DataFrame by accessing the DataFrame.columns
variable. 您可以通过访问DataFrame.columns
变量来找出熊猫DataFrame中存在哪些列。
To access the Series attached to a particular column, you can use the getitem method of the DataFrame []
要访问附加到特定列的系列,可以使用DataFrame []
的getitem方法
Tiny example: 小例子:
col = df.columns[0]
df[col] = [1, 2, 3, 4, 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.