如何使用 pandas 将增量数字添加到新列 [英] How to Add Incremental Numbers to a New Column Using Pandas

查看:74
本文介绍了如何使用 pandas 将增量数字添加到新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简化的数据框:

I have this simplified dataframe:

ID   Fruit
F1   Apple
F2   Orange
F3   Banana 

我想在数据帧的开头添加一个新列df['New_ID'],该列的编号880每行增加1.

I want to add in the begining of the dataframe a new column df['New_ID'] which has the number 880 that increments by one in each row.

输出应该像这样:

New_ID   ID   Fruit
880      F1   Apple
881      F2   Orange
882      F3   Banana  

我尝试了以下操作:

df['New_ID'] = ["880"] # but I want to do this without assigning it the list of numbers literally

有什么办法解决这个问题吗?

Any idea how to solve this?

谢谢!

推荐答案

此处:

df = df.reset_index()
df.columns[0] = 'New_ID'
df['New_ID'] = df.index + 880

这篇关于如何使用 pandas 将增量数字添加到新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆