向 Pandas 数据框添加具有特定项目值的新列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15704274/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:44:53  来源:igfitidea点击:

adding new column to pandas dataframe with values for particular items?

pythonpandas

提问by

I have this pandas dataframe:

我有这个Pandas数据框:

d=pandas.DataFrame([{"a": 1}, {"a": 3, "b": 2}])

and I'm trying to add a new column to it with non-null values only for certain rows, based on their numeric indices in the array. for example, adding a new column "c" only to the first row in d:

并且我试图根据它们在数组中的数字索引,仅针对某些行添加一个具有非空值的新列。例如,仅在 中的第一行添加新列“c” d

# array of row indices
indx = np.array([0])
d.ix[indx]["c"] = "foo"

which should add "foo" as the column "c" value for the first row, and NaN for all other rows. but this doesn't seem to change the array:

它应该添加“foo”作为第一行的列“c”值,并为所有其他行添加 NaN。但这似乎并没有改变数组:

d.ix[np.array([0])]["c"] = "foo"
In [18]: d
Out[18]: 
   a   b
0  1 NaN
1  3   2

what am I doing wrong here? how can it be done? thanks.

我在这里做错了什么?怎么做到呢?谢谢。

采纳答案by Jeff

In [11]: df = pd.DataFrame([{"a": 1}, {"a": 3, "b": 2}])

In [12]: df['c'] = np.array(['foo',np.nan])

In [13]: df
Out[13]: 
   a   b    c
0  1 NaN  foo
1  3   2  nan

If you were assigning a numeric value, the following would work

如果您要分配一个数值,以下内容将起作用

In [16]: df['c'] = np.nan

In [17]: df.ix[0,'c'] = 1

In [18]: df
Out[18]: 
   a   b   c
0  1 NaN   1
1  3   2 NaN