我有以下数据:

    url='https://raw.githubusercontent.com/108michael/ms_thesis/master/clean_gdp_data_all.csv'

c=pd.read_csv(url, index_col=0)
c = c.loc[(c.GeoName == 'California') & \
           (c.ComponentName == 'Real GDP by state')]
c.head(3)


    GeoName     ComponentName   IndustryClassification  Description     2004    2005    2006    2007    2008    2009    2010    2011    2012    2013    2014
38281   California  Real GDP by state   111-112     Farms   15717   18751   18215   15335   14109   18798   19197   16535   15014   16909   0
38282   California  Real GDP by state   113-115     Forestry, fishing, and related activities   6234    6278    7845    7786    7365    7390    7831    8115    8995    9312    0
38284   California  Real GDP by state   211     Oil and gas extraction  7769    8107    10693   12342   12010   17155   14575   15289   18849   16165   0


我想使用for循环运行以下代码,除了我想每年(2004-2014)运行它,然后将它们合并在一起,如最后一行代码所示:

    d = c.sort_values('2004', ascending=False).head(10)[['GeoName', \
'IndustryClassification', 'Description', 'ComponentName', '2004' ]]


e = c.sort_values('2005', ascending=False).head(10)[['GeoName', \
'IndustryClassification', 'Description', 'ComponentName', '2005' ]]

crgdp = pd.merge(d,e, how='inner', on=['GeoName', \
'IndustryClassification', 'Description', 'ComponentName'])

最佳答案

我认为您无法以想要的方式执行此操作,因为一行中的所有值都是“已连接”并属于该行。因此,您可以按一列对DF进行排序,这将对所有行与所有对应的值重新排序,但是下一次当您对另一列进行排序时,您将在第一列中失去排序顺序,依此类推...

在以下示例中,查看索引值以及ab列中的值:

In [16]: df
Out[16]:
   a  b  c
0  0  7  1
1  6  6  0
2  7  4  5

In [17]: df.sort_values(by='a', ascending=False)
Out[17]:
   a  b  c
2  7  4  5
1  6  6  0
0  0  7  1

In [18]: df.sort_values(by='b', ascending=False)
Out[18]:
   a  b  c
0  0  7  1
1  6  6  0
2  7  4  5

In [19]: df.sort_values(by=['a','b'], ascending=False)
Out[19]:
   a  b  c
2  7  4  5
1  6  6  0
0  0  7  1


注意:没关系,我们如何对数据进行排序,每一行中的所有值都相互“绑定”到其索引。

因此,您可以按ab['a','b']对DF进行排序,但是在这种情况下,b列不会单调递减。

然后查看您的数据-如果您将数据按“合并”列进行分组并检查重复项,则将发现其中没有任何重复项:

In [132]: c.groupby(['GeoName', 'IndustryClassification', 'Description', 'ComponentName']).size().nlargest(3)
Out[132]:
GeoName     IndustryClassification  Description       ComponentName
California  ...                     Federal civilian  Real GDP by state    1
                                    Federal military  Real GDP by state    1
                                    State and local   Real GDP by state    1
dtype: int64


它显示每个组只有一行。因此,合并后所有行将保持不变,因为您可以将['GeoName', 'IndustryClassification', 'Description', 'ComponentName']列视为主键(即唯一标识符)。

这是一个例子:

In [125]: c.query("GeoName == 'California' and IndustryClassification == '111-112' and Description == 'Farms' and ComponentName == 'Real GDP by s
tate'")
Out[125]:
          GeoName      ComponentName IndustryClassification Description  \
38281  California  Real GDP by state                111-112       Farms

          2004     2005     2006     2007     2008     2009     2010     2011  \
38281  15717.0  18751.0  18215.0  15335.0  14109.0  18798.0  19197.0  16535.0

          2012     2013  2014
38281  15014.0  16909.0   0.0

关于python - Pandas :使用for循环执行多个命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36824086/

10-09 15:06