我有一个如下所示的df:
User Chat_People Talk_People Schedule_Time
1 first second third
2 second third first
我想将其转换为:
User First Second Third
1 Chat_People Talk_People Schedule_Time
2 Schedule_Time Chat_People Talk_People
我尝试做:
df["First"] = df.apply((lambda x: col_name for col_name in ["Chat_People","Talk_People","Schedule_Time"] if x[col_name] == "first"), axis=1)
但是我遇到了:
TypeError: cannot create 'generator' instances
任何帮助将不胜感激!
答案 0 :(得分:4)
melt
然后pivot
out = df.melt('User').pivot('User','value','variable').reset_index()
out
value User first second third
0 1 Chat_People Talk_People Schedule_Time
1 2 Schedule_Time Chat_People Talk_People