在python中,是否有通过多行/多列键值匹配来获取比率的快捷方式?

在python中,是否有通过多行/多列键值匹配来获取比率的快捷方式?,python,pandas,Python,Pandas,我有世界统计数据中的农业数据,我试图从世界统计数据中得到每种商品各自属性的百分比。我正在考虑可能更容易的操作。我尝试了按列进行除法的方法,但我觉得列的键值匹配和除法是正确的。有人能告诉我什么是最简单的方法来获得列中匹配的条件键值除法吗?有什么想法吗 最小数据量: import pandas as pd df = pd.read_csv('minimal_data.csv') allCommods= df['commodity'].unique().tolist() df_notALL = d

我有世界统计数据中的农业数据,我试图从世界统计数据中得到每种商品各自属性的百分比。我正在考虑可能更容易的操作。我尝试了按列进行除法的方法,但我觉得列的键值匹配和除法是正确的。有人能告诉我什么是最简单的方法来获得列中匹配的条件键值除法吗?有什么想法吗

最小数据量

import pandas as pd

df = pd.read_csv('minimal_data.csv')

allCommods= df['commodity'].unique().tolist()
df_notALL = df[~(df['country']=='World')]
df_wrld = df[(df['country']=='World')]
ctyList_notALL = df_notALL['country'].unique().tolist()
ctyList_wrd = df_wrld['country'].unique().tolist()
yrsList = df['year'].unique().tolist()

s=''
for i,j,k in zip(allCommods,ctyList_notALL, yrsList):
      s += '{}=="{}"&'.format(i,j, k)

w=''
for m,n,l in zip(allCommods, ctyList_wrd, yrsList):
    w += '{}=="{}"&'.format(m,n, l)
这是你的电话号码

我当前的尝试

import pandas as pd

df = pd.read_csv('minimal_data.csv')

allCommods= df['commodity'].unique().tolist()
df_notALL = df[~(df['country']=='World')]
df_wrld = df[(df['country']=='World')]
ctyList_notALL = df_notALL['country'].unique().tolist()
ctyList_wrd = df_wrld['country'].unique().tolist()
yrsList = df['year'].unique().tolist()

s=''
for i,j,k in zip(allCommods,ctyList_notALL, yrsList):
      s += '{}=="{}"&'.format(i,j, k)

w=''
for m,n,l in zip(allCommods, ctyList_wrd, yrsList):
    w += '{}=="{}"&'.format(m,n, l)
在上面的代码中,我尝试对多个列进行键值匹配,首先进行除法,如下所示:

df.div(df_wrld, axis=0).applymap(lambda x: f'{x * 100:.2f}%')
但是我的计算是错误的,似乎我在多个列上进行键值匹配的方法是不正确的,并且操作上面的数据是没有效率的。我怎样才能做到这一点?有没有什么简单的方法来划分这些数据

具体目标

import pandas as pd

df = pd.read_csv('minimal_data.csv')

allCommods= df['commodity'].unique().tolist()
df_notALL = df[~(df['country']=='World')]
df_wrld = df[(df['country']=='World')]
ctyList_notALL = df_notALL['country'].unique().tolist()
ctyList_wrd = df_wrld['country'].unique().tolist()
yrsList = df['year'].unique().tolist()

s=''
for i,j,k in zip(allCommods,ctyList_notALL, yrsList):
      s += '{}=="{}"&'.format(i,j, k)

w=''
for m,n,l in zip(allCommods, ctyList_wrd, yrsList):
    w += '{}=="{}"&'.format(m,n, l)
我想得到每种商品属性占其全球总量的百分比。 例如,历年来每个国家的
大麦
玉米
小麦
的生产率

简单的玩具计算,如:

2010年澳大利亚玉米产量率=2010年澳大利亚玉米产量/2010年世界玉米产量


我想得到一个dataframe的列表,其中每个dataframe可以在全国范围内按年份列出每个属性(如生产、消费、进口、出口)的百分比。如何使熊猫的这种操作更容易?有什么想法吗?

以下是我要做的:

# compute the total production by commodity and country
new_df = (df.groupby(['commodity','country','year'])
            [['production','imports','exports']].sum()
            .unstack('commodity')
        )

# compute the contribution by dividing the `World` production
new_df.div(new_df.loc['World']).drop('World')
输出:

                   production                      ...   exports                    
commodity              Barley      Beef      Corn  ...      Oats      Pork     Wheat
country       year                                 ...                              
Argentina     2010   0.024047  0.044305  0.029666  ...  0.003160  0.000166  0.071362
              2011   0.033761  0.043117  0.023069  ...  0.006318  0.000143  0.081989
              2012   0.038677  0.044310  0.030035  ...  0.000967  0.000137  0.025712
              2013   0.032892  0.047414  0.025316  ...  0.000853  0.000143  0.013560
              2014   0.020473  0.044775  0.028125  ...  0.000851  0.000143  0.032280
...                       ...       ...       ...  ...       ...       ...       ...
United States 2016   0.029606  0.191991  0.341164  ...  0.020602  0.284109  0.155863
              2017   0.021794  0.195829  0.343577  ...  0.014039  0.307980  0.135117
              2018   0.024091  0.196168  0.324269  ...  0.010818  0.315571  0.146805
              2019   0.023717  0.200977  0.312752  ...  0.012104  0.301393  0.148218
              2020        NaN  0.203120       NaN  ...       NaN  0.311118       NaN

[66 rows x 18 columns]

下面是我要做的:

# compute the total production by commodity and country
new_df = (df.groupby(['commodity','country','year'])
            [['production','imports','exports']].sum()
            .unstack('commodity')
        )

# compute the contribution by dividing the `World` production
new_df.div(new_df.loc['World']).drop('World')
输出:

                   production                      ...   exports                    
commodity              Barley      Beef      Corn  ...      Oats      Pork     Wheat
country       year                                 ...                              
Argentina     2010   0.024047  0.044305  0.029666  ...  0.003160  0.000166  0.071362
              2011   0.033761  0.043117  0.023069  ...  0.006318  0.000143  0.081989
              2012   0.038677  0.044310  0.030035  ...  0.000967  0.000137  0.025712
              2013   0.032892  0.047414  0.025316  ...  0.000853  0.000143  0.013560
              2014   0.020473  0.044775  0.028125  ...  0.000851  0.000143  0.032280
...                       ...       ...       ...  ...       ...       ...       ...
United States 2016   0.029606  0.191991  0.341164  ...  0.020602  0.284109  0.155863
              2017   0.021794  0.195829  0.343577  ...  0.014039  0.307980  0.135117
              2018   0.024091  0.196168  0.324269  ...  0.010818  0.315571  0.146805
              2019   0.023717  0.200977  0.312752  ...  0.012104  0.301393  0.148218
              2020        NaN  0.203120       NaN  ...       NaN  0.311118       NaN

[66 rows x 18 columns]

我认为这是不正确的计算,我想按年份计算,比如2010年的生产率、进口、消费等等。我想我已经很清楚地阐明了我的意图。事实上,我们不需要
world
。看起来我们需要循环浏览以下列:
import
production
export
。我们可以改进解决方案吗?谢谢,有什么方法可以将输出的多索引数据帧强制转换到数据帧列表中吗?感谢您可以这样做,例如:
new_df.loc['Argentina','production']
。可以使用
get_level_value
提取这对值。谢谢,我的意思是,可以通过编程轻松地实现这一点吗?有什么诀窍吗?我认为这不是正确的计算方法,我想按年份计算,比如2010年的生产率、进口、消费等等。我想我已经很清楚地阐明了我的意图。事实上,我们不需要
world
。看起来我们需要循环浏览以下列:
import
production
export
。我们可以改进解决方案吗?谢谢,有什么方法可以将输出的多索引数据帧强制转换到数据帧列表中吗?感谢您可以这样做,例如:
new_df.loc['Argentina','production']
。可以使用
get_level_value
提取这对值。谢谢,我的意思是,可以通过编程轻松地实现这一点吗?有什么诀窍吗?