pandas 去重后drop_duplicates索引重置
使用drop_duplicates()方法去除重复值后,可以使用reset_index()方法重置索引。
示例代码如下:
import pandas as pd
# 创建示例数据
df = pd.DataFrame({'A': [1, 2, 2, 3, 4],
'B': ['a', 'b', 'b', 'c', 'd']})
# 去除重复值
df.drop_duplicates(inplace=True)
# 重置索引
df.reset_index(drop=True, inplace=True)
print(df)
输出结果为:
A B
0 1 a
1 2 b
2 3 c
3 4 d
在reset_index()方法中,参数drop=True表示将原来的索引丢弃,使用新的默认索引。
原文地址: https://www.cveoy.top/t/topic/imrQ 著作权归作者所有。请勿转载和采集!