如何使用 Pandas 将二维表(DataFrame)反转为一维列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27764378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reverse a 2-dimensional table (DataFrame) into a 1 dimensional list using Pandas?
提问by Ning Chen
I am looking in Python/Pandas for a tip that reverses a 2-dimension table into 1 dimensional list.
我正在 Python/Pandas 中寻找将二维表反转为一维列表的提示。
I usually leverage an Excel function to do it, but I believe that there is a smart Python way to do it.
我通常利用 Excel 函数来做这件事,但我相信有一种聪明的 Python 方法可以做到这一点。
Step
步


More details of the Excel way: http://www.extendoffice.com/documents/excel/2461-excel-reverse-pivot-table.html
Excel方式的更多细节:http: //www.extendoffice.com/documents/excel/2461-excel-reverse-pivot-table.html
回答by Alex Riley
This type of operation could also be done using pd.melt, which unpivots a DataFrame.
这种类型的操作也可以使用 来完成pd.melt,它可以对DataFrame 进行反透视。
If the DataFrame dflooks like this:
如果 DataFramedf如下所示:
row labels Tue Wed Thu Sat Sun Fri Mon
0 Apple 21 39 24 27 37 46 42
1 Banana 32 50 48 35 21 27 22
2 Pear 37 20 45 45 31 50 32
Then we select the row_labelscolumn to be our id_varand the rest of the columns to be our values (value_vars). We can even choose the new names for the columns at the same time:
然后我们选择row_labels列作为我们id_var的列,其余列作为我们的值 ( value_vars)。我们甚至可以同时为列选择新名称:
>>> pd.melt(df,
id_vars='row labels',
value_vars=list(df.columns[1:]), # list of days of the week
var_name='Column',
value_name='Sum of Value')
row labels Column Sum of Value
0 Apple Tue 21
1 Banana Tue 32
2 Pear Tue 37
3 Apple Wed 39
4 Banana Wed 50
5 Pear Wed 20
...
The value_varsare stacked below each other: if the column values need to be in a particular order it will be necessary to sort the columns after melting.
所述value_vars堆叠下面相互:如果列值需要以特定的顺序将需要熔化之后对列进行排序。
回答by tmr232
This should do the trick:
这应该可以解决问题:
table = [
["Lables", "A", "B", "C"],
["X", 1, 2, 3],
["Y", 4, 5, 6],
["Z", 7, 8, 9]
]
new_table = [["Row", "Column", "Data"]]
for line in table[1:]:
for name, cell in zip(table[0], line)[1:]:
new_line = [line[0], name, cell]
new_table.append(new_line)
The output is:
输出是:
[
['Row', 'Column', 'Data'],
['X', 'A', 1],
['X', 'B', 2],
['X', 'C', 3],
['Y', 'A', 4],
['Y', 'B', 5],
['Y', 'C', 6],
['Z', 'A', 7],
['Z', 'B', 8],
['Z', 'C', 9]
]
回答by user2754799
Example taken from http://pandas.pydata.org/pandas-docs/stable/reshaping.html
示例取自http://pandas.pydata.org/pandas-docs/stable/reshaping.html
tl;dr, use:
tl;博士,使用:
from pandas import *
df.stack()
====================
====================
Let's give an example of how this can be done.
让我们举一个例子来说明如何做到这一点。
Generate the sample data first:
先生成样本数据:
from pandas import *
import pandas.util.testing as tm; tm.N = 3
import numpy as np
def unpivot(frame):
N, K = frame.shape
data = {'value' : frame.values.ravel('F'),
'variable' : np.asarray(frame.columns).repeat(N),
'date' : np.tile(np.asarray(frame.index), K)}
return DataFrame(data, columns=['date', 'variable', 'value'])
df = unpivot(tm.makeTimeDataFrame())
df2= df.pivot('date', 'variable')
We will unpivot this table:
我们将取消透视此表:
value
variable A B C D
date
2000-01-03 -0.425081 0.163899 -0.216486 -0.266285
2000-01-04 0.078073 0.581277 0.103257 -0.338083
2000-01-05 0.721696 -1.311509 -0.379956 0.642527
Run:
跑:
df2= df.pivot('date', 'variable')
print df2
Voila! Now we get the desired table.
瞧!现在我们得到了想要的表。
value
date variable
2000-01-03 A -0.425081
B 0.163899
C -0.216486
D -0.266285
2000-01-04 A 0.078073
B 0.581277
C 0.103257
D -0.338083
2000-01-05 A 0.721696
B -1.311509
C -0.379956
D 0.642527

