pandas 通过分解旧数据框的列数据来创建新数据框

rvpgvaaj  于 2023-05-05  发布在  其他
关注(0)|答案(3)|浏览(76)

我有下面的数据框
| 推销员|北通路|南向通道|东通路|西通路|
| --------------|--------------|--------------|--------------|--------------|
| 一个|1|0|0|1|
| B|0|1|1|1|
| C类|1|0|1|1|
我想把上面的转换成下面的格式
| 推销员|方向|存取|
| --------------|--------------|--------------|
| 一个|北|1|
| 一个|南|0|
| 一个|东|0|
| 一个|西|1|
| B|北|0|
| B|南|1|
| B|东|1|
| B|西|1|
我尝试探索分裂和转置函数,但没有得到预期的结果。有人可以帮助代码在Python中进行上述更改吗,提前感谢。

w1e3prcc

w1e3prcc1#

另一种解决方案(使用pd.wide_to_long):

df.columns = [f'access_{c.split("_")[0]}' if "_access" in c else c for c in df.columns]

x = pd.wide_to_long(
    df, stubnames="access", suffix=r".*", i=["salesman"], j="direction", sep="_"
).reset_index()
print(x)

图纸:

salesman direction  access
0         A     north       1
1         B     north       0
2         C     north       1
3         A     south       0
4         B     south       1
5         C     south       0
6         A      east       0
7         B      east       1
8         C      east       1
9         A      west       1
10        B      west       1
11        C      west       1
kqqjbcuj

kqqjbcuj2#

您可以用途:

>>> (df.set_index('salesman').rename(columns=lambda x: x.split('_')[0])
       .rename_axis(columns='direction').stack().rename('access').reset_index())

   salesman direction  access
0         A     north       1
1         A     south       0
2         A      east       0
3         A      west       1
4         B     north       0
5         B     south       1
6         B      east       1
7         B      west       1
8         C     north       1
9         C     south       0
10        C      east       1
11        C      west       1

melt

>>> (df.rename(columns=lambda x: x.split('_')[0])
       .melt('salesman', var_name='direction', value_name='access')
       .sort_values('salesman', kind='stable', ignore_index=True))

# OR

>>> (df.melt('salesman', var_name='direction', value_name='access')
       .sort_values('salesman', kind='stable', ignore_index=True)
       .assign(direction=lambda x: x['direction'].str.rstrip('_access')))

   salesman direction  access
0         A     north       1
1         A     south       0
2         A      east       0
3         A      west       1
4         B     north       0
5         B     south       1
6         B      east       1
7         B      west       1
8         C     north       1
9         C     south       0
10        C      east       1
11        C      west       1
fcg9iug3

fcg9iug33#

一个选项是janitor.pivot_longer:

# pip install pyjanitor
import pandas as pd
import janitor

df.pivot_longer(index = 'salesman', 
                names_to = ('direction', '.value'), 
                names_sep = '_')
   salesman direction  access
0         A     north       1
1         B     north       0
2         C     north       1
3         A     south       0
4         B     south       1
5         C     south       0
6         A      east       0
7         B      east       1
8         C      east       1
9         A      west       1
10        B      west       1
11        C      west       1

相关问题