使用组约束将列值向左移动 [英] Shift column values to the left with group constraints
本文介绍了使用组约束将列值向左移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个潜在的"大型DataFrame,
I have a "potentially" large DataFrame,
A B_1 B_2 B_3 C_1 C_2 C_3
0 231 text2 text3 NaN date4 date1 NaN
1 443 NaN NaN text1 date2 NaN NaN
2 456 text1 text1 text2 NaN date3 date1
为了最小化某些NaN
,我想将所有数据向左移动,从而能够忽略所有NaN
列.不过,此移位必须保留在适当的组内,这意味着,只要单元格不在B_1或B_2列中,只要它不移位到C_1等就无关紧要.
In order to minimize some of the NaN
s I want to shift all the data to the left and thus be able to disregard all NaN
columns. This shift though must remain within the appropriate group, meaning that it does not matter if a cell is in column B_1 or B_2, as long as it does not get shifted to C_1 etc.
我要结束的是这个
A B_1 B_2 B_3 C_1 C_2
0 231 text2 text3 NaN date4 date1
1 443 text1 NaN NaN date2 NaN
2 456 text1 text1 text2 date3 date1
推荐答案
每组使用调整功能,仅必要MultiIndex
在列中:
Use justify function per groups, only necessary MultiIndex
in columns:
df = df.set_index('A')
df.columns = df.columns.str.split('_', expand=True)
f = lambda x: pd.DataFrame(justify(x.values, invalid_val=np.nan),
index=x.index, columns=x.columns)
df = df.groupby(axis=1, level=0).apply(f)
print (df)
B C
1 2 3 1 2 3
A
231 text2 text3 NaN date4 date1 NaN
443 text1 NaN NaN date2 NaN NaN
456 text1 text1 text2 date3 date1 NaN
然后:
df1.columns = [f'{a}_{b}' for a, b in df1.columns]
df1 = df1.reset_index()
结合了先前的答案中的解决方案:
Combined with solution from previous answer:
g = df.groupby('A').cumcount() + 1
df1 = df.set_index(['A', g]).unstack()
f = lambda x: pd.DataFrame(justify(x.values, invalid_val=np.nan),
index=x.index, columns=x.columns)
df1 = df.groupby(axis=1, level=0).apply(f)
df1.columns = [f'{a}_{b}' for a, b in df1.columns]
df1 = df1.reset_index()
这篇关于使用组约束将列值向左移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文