[英]Auto increment index in pandas
How can I auto increment indexes in pandas and even if I delete the existing record then while appending new record the indexes should not get reallocated or the index once alloted to a record should not change? 我如何在熊猫中自动递增索引,即使删除现有记录,然后在追加新记录时,索引也不应重新分配,或者一旦分配给记录,索引就不应更改? I have tried
append
with both of its options and it changes the index. 我尝试了带有两个选项的
append
,它会更改索引。 Feature somewhat like the MySQL AUTO_INCREMENT
will be of great help. 类似于MySQL
AUTO_INCREMENT
将有很大的帮助。
Pandas is not a database. 熊猫不是数据库。 Deleting and inserting single rows has disastrous performance.
删除和插入单行会造成灾难性的后果。 Instead, remove rows by masking them out:
而是通过屏蔽掉行来删除它们:
maxidx = df.index[-1]
mask = [True, False, True] # etc
df = df[mask]
The retained rows will keep their original index values. 保留的行将保留其原始索引值。
Then to add multiple rows at once: 然后一次添加多行:
to_add = # some dataframe
to_add.index = pd.RangeIndex(maxidx + 1, maxidx + 1 + len(to_add))
df = pd.concat([df, to_add])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.