列到大熊猫的行

时间:2016-06-15 16:22:18

标签: python pandas dataframe transpose

我正在尝试在pandas中执行以下操作。关于pandas这样做的任何建议?

In [1]: input  = pd.DataFrame({"X_1": [1], "X_2": [2], "X_3": [5], "Y_1": [1.2], "Y_2": [2.3], "Y_3": [3.4]})

In [2]: input
Out[2]: 
   X_1  X_2  X_3  Y_1  Y_2  Y_3
0    1    2    5  1.2  2.3  3.4

In [3]: output = pd.DataFrame({"X": [1,2,5], "Y": [1.2, 2.3, 3.4]})

In [4]: output
Out[4]: 
   X    Y
0  1  1.2
1  2  2.3
2  5  3.4

4 个答案:

答案 0 :(得分:4)

使用str.splitstack

df.columns = df.columns.str.split('_', expand=True)
df.stack().loc[0]

   X    Y
1  1  1.2
2  2  2.3
3  5  3.4

注意:索引是[1, 2, 3]与原始列匹配。

答案 1 :(得分:1)

可能不是最佳答案,但您可以执行以下操作:

a = {"X_1": [1], "X_2": [2], "X_3": [5], "Y_1": [1.2], "Y_2": [2.3], "Y_3": [3.4]}
x = [a[key][0] for key in sorted(a.keys()) if 'X' in key]  
y = [a[key][0] for key in sorted(a.keys()) if 'Y' in key]
df = pd.DataFrame([x, y]).T

得到以下特性:

     0    1
0  1.0  1.2
1  2.0  2.3
2  5.0  3.4

答案 2 :(得分:1)

您可以在_之前添加split个列,并创建unique个值ab。然后创建MultiIndex.from_productstack

cols = input.columns.str.split('_')
print (cols)
Index([['X', '1'], ['X', '2'], ['X', '3'], ['Y', '1'], 
       ['Y', '2'], ['Y', '3']], dtype='object')

a = cols.str[0].unique()
print (a)
['X' 'Y']

b = cols.str[1].unique()
print (b)
['1' '2' '3']

input.columns = pd.MultiIndex.from_product([a,b])
print (input.stack(1).reset_index(drop=True))
   X    Y
0  1  1.2
1  2  2.3
2  5  3.4

答案 3 :(得分:1)

对于这类事情,我更喜欢melt后跟字符串操作,然后是pivot

df = pd.melt(input)
df[['column', 'index']] = df['variable'].str.split('_', expand=True)
df = df.pivot(index='index', columns='column', values='value')
print(df)

输出:

column    X    Y
index           
1       1.0  1.2
2       2.0  2.3
3       5.0  3.4
相关问题