dataframe 删除重复行
可以使用 pandas 的 drop_duplicates() 方法来删除重复行。
示例代码:
import pandas as pd
# 创建一个包含重复行的 DataFrame
df = pd.DataFrame({
'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'two', 'two', 'one', 'one', 'two'],
'C': [1, 2, 1, 2, 1, 2, 1, 1]
})
print(df)
# 删除重复行
df.drop_duplicates(inplace=True)
print(df)
输出:
A B C
0 foo one 1
1 bar one 2
2 foo two 1
3 bar two 2
4 foo two 1
5 bar one 2
6 foo one 1
7 foo two 1
A B C
0 foo one 1
1 bar one 2
2 foo two 1
3 bar two 2
``
原文地址: https://www.cveoy.top/t/topic/fhsX 著作权归作者所有。请勿转载和采集!