pandas 中的列到行
Columns to Rows in pandas
我正在尝试在 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
可能不是最佳答案,但您可以执行以下操作:
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
你可以先split
columns by _
and create unique
values a
and b
. Then create MultiIndex.from_product
and stack
:
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
使用str.split
和stack
。
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]
匹配原始列。
对于这种事情,我更喜欢 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
我正在尝试在 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
可能不是最佳答案,但您可以执行以下操作:
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
你可以先split
columns by _
and create unique
values a
and b
. Then create MultiIndex.from_product
and stack
:
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
使用str.split
和stack
。
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]
匹配原始列。
对于这种事情,我更喜欢 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