如何使用修改后的 bfill pandas 将附近的重复项折叠成一行

发布于 2025-01-15 05:46:00 字数 1032 浏览 0 评论 0原文

我有一个如下所示的数据框,

ID,F1,F2,F3,F4,F5,F6,L1,L2,L3,L4,L5,L6
1,X,,X,,,X,A,B,C
1,X,,X,,,X,A,B,C
1,X,,X,,,X,A,B,C
2,X,,,X,,X,A,B,C,D,E
3,X,X,X,,,X,A
3,X,X,X,,,X,,B,,C
3,X,X,X,,,X,,D,C
4,X,X,,,,,A,B
4,,,,X,,X,G,H,I
4,,,X,,,,T

df = pd.read_clipboard(sep=',')

我想执行以下操作:

a)删除完整的重复项(其中每列的所有值都匹配)。 例如:ID=1 (keep=first)

b) 将附近的重复项折叠成一行。 例如:ID= 3 和 4。接近重复是仅 ID 匹配但其余 F 编号和 L 编号列不同的行

我尝试执行以下操作,但会导致不正确的输出

下面的代码未复制其他 L 编号值之前没有 NA

df = df.drop_duplicates(keep='first') # this drops full duplicates ex:ID = 1
df.groupby(['ID'])['ID','F1','F2','F3','F4','F5','F6','L1','L2','L3','L4','L5','L6'].bfill().drop_duplicates(subset=['ID'],keep='first') 

在实际数据中,有 50 个 F 列和 50 个 L 列。对于F列,X的位置很重要并且必须正确,而对于L列,它可以在任何地方,只要它被捕获就可以了。

我希望我的输出如下所示

在此处输入图像描述

I have a dataframe like as shown below

ID,F1,F2,F3,F4,F5,F6,L1,L2,L3,L4,L5,L6
1,X,,X,,,X,A,B,C
1,X,,X,,,X,A,B,C
1,X,,X,,,X,A,B,C
2,X,,,X,,X,A,B,C,D,E
3,X,X,X,,,X,A
3,X,X,X,,,X,,B,,C
3,X,X,X,,,X,,D,C
4,X,X,,,,,A,B
4,,,,X,,X,G,H,I
4,,,X,,,,T

df = pd.read_clipboard(sep=',')

I would like to do the below

a) Remove full duplicates (where all values of each column match). ex: ID=1 (keep=first)

b) Collapse near duplicates into one row. ex: ID= 3 and 4. Near duplicates are rows where only ID match but rest of the F numbered and L number columns differ

I was trying the below but it results in incorrect output

The below code misses to copy other L numbered values which doesn't have NA before

df = df.drop_duplicates(keep='first') # this drops full duplicates ex:ID = 1
df.groupby(['ID'])['ID','F1','F2','F3','F4','F5','F6','L1','L2','L3','L4','L5','L6'].bfill().drop_duplicates(subset=['ID'],keep='first') 

In real data, there are 50 F columns and 50 L columns. For F columns the position of X is important and has to be correct whereas for L columns, it can be anywhere as long as it is captured, it is fine.

I expect my output to be like as shown below

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2025-01-22 05:46:00

使用:

#first omit all duplicates by all columns
df = df.drop_duplicates(keep='first')

cL = df.filter(like='L').columns
cF = df.filter(like='F').columns

def f(x):
     s =  pd.Series(x.stack().unique()).rename(lambda x: f'L{x + 1}')
     print (s)
     return s

#recreate L columns by remove missing values and duplicates
#f = lambda x: pd.Series(x.stack().unique()).rename(lambda x: f'L{x + 1}')
df1 = df[cL].groupby(df['ID']).apply(f).unstack()

#remove original L columns
df = df.drop(cL, axis=1)
#for F columns processing with original solution
df[cF] = df.groupby(['ID'])[cF].bfill()
#after remove duplicates for F columns add L columns in df1
df = df.drop_duplicates(subset=['ID'],keep='first').join(df1, on='ID')
print (df)
   ID F1   F2   F3   F4  F5 F6 L1 L2 L3   L4   L5   L6
0   1  X  NaN    X  NaN NaN  X  A  B  C  NaN  NaN  NaN
3   2  X  NaN  NaN    X NaN  X  A  B  C    D    E  NaN
4   3  X    X    X  NaN NaN  X  A  B  C    D  NaN  NaN
7   4  X    X    X    X NaN  X  A  B  G    H    I    T

Use:

#first omit all duplicates by all columns
df = df.drop_duplicates(keep='first')

cL = df.filter(like='L').columns
cF = df.filter(like='F').columns

def f(x):
     s =  pd.Series(x.stack().unique()).rename(lambda x: f'L{x + 1}')
     print (s)
     return s

#recreate L columns by remove missing values and duplicates
#f = lambda x: pd.Series(x.stack().unique()).rename(lambda x: f'L{x + 1}')
df1 = df[cL].groupby(df['ID']).apply(f).unstack()

#remove original L columns
df = df.drop(cL, axis=1)
#for F columns processing with original solution
df[cF] = df.groupby(['ID'])[cF].bfill()
#after remove duplicates for F columns add L columns in df1
df = df.drop_duplicates(subset=['ID'],keep='first').join(df1, on='ID')
print (df)
   ID F1   F2   F3   F4  F5 F6 L1 L2 L3   L4   L5   L6
0   1  X  NaN    X  NaN NaN  X  A  B  C  NaN  NaN  NaN
3   2  X  NaN  NaN    X NaN  X  A  B  C    D    E  NaN
4   3  X    X    X  NaN NaN  X  A  B  C    D  NaN  NaN
7   4  X    X    X    X NaN  X  A  B  G    H    I    T
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文