2024-09-28 15:29:34 发布
网友
我现在有一个像这样的数据帧
User Date FeatureA FeatureB John DateA 1 2 John DateB 3 5
不管怎样,我能把这两行合并成一行吗
User Date1 Date2 FeatureA1 FeatureB1 FeatureA2 FeatureB2 John DateA DateB 1 2 3 5
我认为需要:
g = df.groupby(['User']).cumcount() df = df.set_index(['User', g]).unstack() df.columns = ['{}{}'.format(i, j+1) for i, j in df.columns] df = df.reset_index() print (df) User Date1 Date2 FeatureA1 FeatureA2 FeatureB1 FeatureB2 0 John DateA DateB 1 3 2 5
说明:
User
MultiIndex
index
我认为需要:
说明:
User
s获取每个组的计数MultiIndex
MultiIndex
index
转换为列相关问题 更多 >
编程相关推荐