【发布时间】:2019-09-30 18:25:32
【问题描述】:
我有一个数据框,其中有一些重复。它们位于每一行的特定数量的列索引中:
df_in
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19...
1 3 4 6 0 2 0 3 0 2 0 3 4 5 6 2 4 5 6 2...
.
.
在索引4-7 的row 1 中,索引8-11 中的[0, 2, 0, 3] 重复,然后索引12-15 中的[4, 5, 6, 2] 重复来自16-19。
我需要检测每行中的每个 4 numbers 是否相等,如果是,则从 DataFrame 中删除其中一个重复。
输出将是:
df_out
0 1 2 3 4 5 6 7 8 9 10 11...
1 3 4 6 0 2 0 3 4 5 6 2...
.
.
伪代码类似于:
for index in range(4, len(df_in.columns)):
if bool((df_in.iloc[:, index] == (df_in.iloc[:, index+4]).all()) == True:
remove either df_in.iloc[:, index] or df_in.iloc[:, index]+4 and keep one
if bool((df_in.iloc[:, index] == (df_in.iloc[:, index+4]).all()) == False:
keep df_in.iloc[:, index]
有没有简单的方法来完成这项工作?
【问题讨论】:
-
应该是
for index in range(4, len(df_in.columns), 4):?
标签: python python-3.x pandas dataframe duplicates