创建名为"Id"的列基于行索引 [英] Create column named "Id" based on row index
本文介绍了创建名为"Id"的列基于行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为名为"Id"的数据框创建一个新列,其中值是行索引+1. 我想像下面的例子:
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 Col1 ...
0 1 a ...
1 2 b ...
2 3 c ...
推荐答案
您可以在索引中添加一个并将其分配给id
列:
You can add one to the index and assign it to the id
column:
df = pd.DataFrame({"Col1": list("abc")})
df["id"] = df.index + 1
df
#Col1 id
#0 a 1
#1 b 2
#2 c 3
这篇关于创建名为"Id"的列基于行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文