pandas 根据行索引创建名为“Id”的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41125138/
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-14 02:37:06 来源:igfitidea点击:
Create column named "Id" based on row index
提问by hdatas
I would like to create a new column for my dataframe named "Id" where the value is the row index +1. I would like to be like the example below:
我想为名为“Id”的数据框创建一个新列,其中值是行索引 +1。我想像下面的例子:
ID Col1 ...
0 1 a ...
1 2 b ...
2 3 c ...
回答by Psidom
You can add one to the index and assign it to the id
column:
您可以向索引添加一个并将其分配给id
列:
df = pd.DataFrame({"Col1": list("abc")})
df["id"] = df.index + 1
df
#Col1 id
#0 a 1
#1 b 2
#2 c 3