在python中使用pandas时,可以使用drop_duplicates
删除相似的行。有没有办法将数据帧分成两个数据帧而不是实际“丢弃”行?
答案 0 :(得分:1)
如果要按重复项拆分数据帧,也许可以使用.duplicated()
返回的布尔数组:
>>> df = pd.DataFrame({"A": [1,1,2,3,2,4]})
>>> df
A
0 1
1 1
2 2
3 3
4 2
5 4
[6 rows x 1 columns]
>>> df_a, df_b= df[~df.duplicated()], df[df.duplicated()]
>>> df_a
A
0 1
2 2
3 3
5 4
[4 rows x 1 columns]
>>> df_b
A
1 1
4 2
[2 rows x 1 columns]