Python 如何在 Pandas 中用一个值填充一列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34811971/
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 do I fill a column with one value in Pandas?
提问by Dervin Thunk
I have a column with consecutive digits in a Pandas DataFrame.
我在 Pandas DataFrame 中有一个连续数字的列。
A
1
2
3
4
I would like to change all those values to a simple string, say "foo", resulting in
我想将所有这些值更改为一个简单的字符串,比如“foo”,导致
A
foo
foo
foo
foo
采纳答案by EdChum
Just select the column and assign like normal:
只需选择列并像往常一样分配:
In [194]:
df['A'] = 'foo'
df
Out[194]:
A
0 foo
1 foo
2 foo
3 foo
Assigning a scalar value will set all the rows to the same scalar value
分配标量值会将所有行设置为相同的标量值
回答by mm_
The good answer above throws a warning. You can also do:
上面的好答案会发出警告。你也可以这样做:
df.insert(0, 'A', 'foo')
where 0 is the index where the new column will be inserted.
其中 0 是将插入新列的索引。