将Pandas中的非结构化行转置

3

我有一个类似这样的数据集:

category                 UK             US           Germany  
sales                    100000        48000        36000 
budget                   50000         20000        14000
n_employees              300           123          134  
diversified              1             0            1   
sustainability_score     22.8          38.9         34.5
e_commerce               37000         7000         11000   
budget                   25000         10000        10000
n_employees              18            22           7  
traffic                  150 mil       38 mil       12500 
subsidy                  33000         26000        23000  
budget                   14000         6000         6000
own_marketing            0             0            1

在数据集中,销售变量对应于总部的销售额。
e_commerce 是电子商务销售额,e_commerce 后面的 budget 其实是公司电子商务部门的预算。同样,subsidy 变量对应于补贴的销售额,subsidy 后面的 budget 变量是该补贴的预算。我想将数据集转换为以下格式(以英国为例):
UK_main_sales UK_main_budget ... UK_e_commerce_sales UK_e_commerce_budget ...
100000        500000             37000               250000

等等。我试图通过跟踪预算变量来对不同部门的变量进行分类,因为它总是紧随部门之后,但我没有成功。

英国的完整变量列表应该像这样:

UK_main_sales
UK_main_budget
UK_main_n_employees
UK_main_diversified
UK_main_sustainability_score 
UK_e_commerce (we could also add sales but I think it is simpler without sales)
UK_e_commerce_budget
UK_e_commerce_n_employees
UK_e_commerce_traffic
UK_subsidy
UK_subsidy_budget
UK_subsidy_own_marketing

有什么想法吗?
1个回答

2

我认为需要:

#get boolean mask for rows for split
mask = df['category'].isin(['subsidy', 'e_commerce'])

#create NaNs for non match values by where
#replace NaNs by forward fill, first NaNs replace by fillna
#create mask for match values by mask and replace by empty string
#join together 
df['category'] = (df['category'].where(mask).ffill().fillna('main').mask(mask).fillna('') 
                   + '_' + df['category']).str.strip('_')

#reshape by unstack 
df = df.set_index('category').unstack().to_frame().T
#flatten MultiIndex
df.columns = df.columns.map('_'.join)

print (df)
  UK_main_sales UK_main_budget UK_main_n_employees UK_main_diversified  \
0        100000          50000                 300                   1   

  UK_main_sustainability_score UK_e_commerce UK_e_commerce_budget  \
0                         22.8         37000                25000   

  UK_e_commerce_n_employees UK_e_commerce_traffic UK_subsidy  \
0                        18               150 mil      33000   

             Germany_main_n_employees  \
0              ...                                   134   

  Germany_main_diversified Germany_main_sustainability_score  \
0                        1                              34.5   

  Germany_e_commerce Germany_e_commerce_budget Germany_e_commerce_n_employees  \
0              11000                     10000                              7   

  Germany_e_commerce_traffic Germany_subsidy Germany_subsidy_budget  \
0                      12500           23000                   6000   

  Germany_subsidy_own_marketing  
0                             1  

[1 rows x 36 columns]

抱歉,请给我一秒钟。 - jezrael
你只想为“budget”更改名称吗? - jezrael
让我编辑问题,非常感谢您抽出时间。 - edyvedy13
非常感谢您的回答,我已经接受了它,但是在一些CSV文件中,它们使用“ecommerce”而不是“e_commerce”,或者使用“subsidy23”而不是“subsidy”。唯一共同的事情是“budget”总是出现在这些类别之后。如果我要求您修改答案,那会太麻烦吗? - edyvedy13
@jezreal 我知道,但有时它们甚至更奇怪,他们称之为电子商务、在线业务。有太多的 CSV 文件。我知道,我应该早点提到这一点... - edyvedy13
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接