2
二维数据帧我有一个二维的数据帧,为了简单起见,它看起来像:排序使用熊猫
df = pd.DataFrame([(1,2.2,5),(2,3,-1)], index=['row1', 'row2'], columns = ["col1","col2",'col3'])
与输出:
col1 col2 col3
row1 1 2.2 5
row2 2 3.0 -1
什么是通过订购的最好办法值来获得:
RowName ColName Value
row2 col3 -1
row1 col1 1
row2 col1 2
row1 col2 2.2
row2 col2 3.0
row1 col3 5
我曾尝试使用.STACK(),并没有走得很远,构造此使用嵌套的for循环是可能的 - 但不雅..
任何想法吗?
Keeth的建议也适用;评论以供将来参考给其他用户:df.stack()。sort()与Keeth的定义不同(即定义'堆积'然后排序它)。前者(这是我的问题)返回'None'。 –
GG_Python我认为你的问题是,在这种情况下,df.stack()返回一个Series和[Series.sort()](http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series。 sort.html)是一个不带任何值的就地操作。 – Keeth