我有一个df,在'transpoting'df:
之后看起来像这样 1 2 3 4 5
level_1 questionId type value exportLabel questionId
0 participantId id -Ll4truw3KbSjVRtXmJy Viewed viewTime
6 7 8
level_1 type value exportLabel
0 time 2019-07-31T02:41:34.063Z Started
如果名称匹配并将其制成列?
如何合并值level_1
索引行?
新的DF应该看起来像这样:
questionId type value exportLabel
0 participantId id -Ll4truw3KbSjVRtXmJy Viewed
1 viewTime time 2019-07-31T02:41:34.063Z Started
我尝试了groupby
,但是数据将转换为串联,索引变成列,并使用此代码:
df = df.groupby(df.loc['level_1'])
df (your data).. Generated using below
temp = StringIO("""
1 2 3 4 5 6 7 8
level_1 questionId type value exportLabel questionId type value exportLabel
0 participantId id -Ll4truw3KbSjVRtXmJy Viewed viewTime time 2019-07-31T02:41:34.063Z Started
""")
df = pd.read_csv(temp, sep='s+')
##df
1 2 3 4 5
level_1 questionId type value exportLabel questionId
0 participantId id -Ll4truw3KbSjVRtXmJy Viewed viewTime
6 7 8
level_1 type value exportLabel
0 time 2019-07-31T02:41:34.063Z Started
df = df.T.groupby('level_1')['0'].apply(lambda x: pd.Series(list(x))).unstack().T
del df.columns.name
print(df[['questionId','type','value','exportLabel']])
questionId type value exportLabel
0 participantId id -Ll4truw3KbSjVRtXmJy Viewed
1 viewTime time 2019-07-31T02:41:34.063Z Started