我需要把我的熊猫数据帧转换成一个奇怪的列表。我有以下熊猫数据帧示例:
输入数据帧:
mydf= pd.DataFrame.from_dict({'ARS':['xx2','xx3','xx1'], 'xyz':['yy1','xx2','xx3'], 'ppp':['xx3','yy2','xx2']}, orient='columns')
mydf= mydf.stack().reset_index()
mydf.columns= ['list1','list2','list3']
newdf= mydf[['list2','list3']]
newdf
list2 list3
0 ARS xx2
1 ppp xx3
2 xyz yy1
3 ARS xx3
4 ppp yy2
5 xyz xx2
6 ARS xx1
7 ppp xx2
8 xyz xx3
所需数据帧:
^{pr2}$有没有人有一个简单的熊猫转换方法?
以下是我的尝试:
我相信一定有更优雅的解决方案。在
以下是Pandas使用
groupby
,pd.concat
和索引的方法:输出:
^{pr2}$如果您真的想要那个“>;”符号,请使用以下内容:
输出:
相关问题 更多 >
编程相关推荐