[英]How to convert an M x M pandas DataFrame into an N X 2 Dataframe?
I have a pandas DataFrame like below:我有一个如下所示的 Pandas DataFrame:
df = pd.DataFrame({"type": ["A", "B", "C"],
"A": [0, 0, 12],
"B": [1, 3, 0],
"C": [0, 1, 1]}
)
I want to transform this to a DataFrame that is NX 2, where I concatenate the column and type
values with " - "
as delimiter.我想将其转换为 NX 2 的 DataFrame,在其中我连接列并使用" - "
作为分隔符type
值。 The output should look like this:输出应如下所示:
pair value
A - A 0
A - B 0
A - C 12
B - A 1
B - B 3
B - C 0
C - A 0
C - B 1
C - C 1
I don't know if there is a name for what I want to accomplish (I thought about pivoting but I believe that is something else), so that didn't help me in googling the solution for this.我不知道我想要完成的事情是否有一个名称(我考虑过旋转,但我相信那是别的东西),所以这对我在谷歌上搜索解决方案没有帮助。 How to solve this problem efficiently?如何有效地解决这个问题?
1st set index as type
and then unstack
and convert the result to dataframe.第一个将索引设置为type
,然后unstack
并将结果转换为数据帧。
try:尝试:
x = df.set_index('type').unstack().to_frame('value')
x.index = x.index.map(' - '.join)
res = x.rename_axis('pair').reset_index()
res:资源:
pair value
0 A - A 0
1 A - B 0
2 A - C 12
3 B - A 1
4 B - B 3
5 B - C 0
6 C - A 0
7 C - B 1
8 C - C 1
First melt the column type
, then join variable
, and type
column with a hyphen -
, and take the required columns only:首先融化列type
,然后加入variable
,并用连字符-
type
列,并仅获取所需的列:
>>> out = df.melt(id_vars='type')
>>> out.assign(pair=out['variable']+'-'+out['type'])[['pair', 'value']]
pair value
0 A-A 0
1 A-B 0
2 A-C 12
3 B-A 1
4 B-B 3
5 B-C 0
6 C-A 0
7 C-B 1
8 C-C 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.