按字母顺序排列熊猫系列

时间:2016-12-25 02:48:19

标签: python pandas

我有2个熊猫系列如下:

                                        indicator_name    
1                                 6-Month Bill Auction
2                                  7-Year Note Auction
3                        ADP Nonfarm Employment Change
4                                        All Car Sales
5                                      All Truck Sales
6                           API Weekly Crude Oil Stock
7                   API Weekly Cushing Crude Oil Stock
8                        API Weekly Distillates Stocks
9                            API Weekly Gasoline Stock
10                       Average Hourly Earnings (MoM)
11                                Average Weekly Hours

...

                                    indicator_name
1                            ADP Employment Change
2                      Advance Goods Trade Balance
3                             Advance Retail Sales
4                      Average Hourly Earnings MoM
5                      Average Hourly Earnings YoY
6               Average Weekly Hours All Employees
7                      Avg Hourly Earning MOM Prod
8                      Avg Hourly Earning YOY Prod
9                      Avg Weekly Hours Production

我想将它们组合起来,以便按以下方式按字母顺序对齐:

1   6-Month Bill Auction                 null
2   7-Year Note Auction                  null
3   null                                 ADP Employment Change
4   ADP Nonfarm Employment Change        Advance Goods Trade Balance
5   null                                 Advance Retail Sales
6   All Car Sales                        null
7   All Truck Sales                      null
8   API Weekly Crude Oil Stock           null
9   API Weekly Cushing Crude Oil Stock   null
10  API Weekly Distillates Stocks        null
11  API Weekly Gasoline Stock            null
12  Average Hourly Earnings (MoM)        Average Hourly Earnings MoM
13  null                                 Average Hourly Earnings YoY
14  Average Weekly Hours                 Average Weekly Hours All Employees
15  null                                 Avg Hourly Earning MOM Prod
16  null                                 Avg Hourly Earning YOY Prod
17  null                                 Avg Weekly Hours Production

有关如何迅速完成任何建议吗? THX!

1 个答案:

答案 0 :(得分:1)

假设您有一个列数据帧。

df = pd.DataFrame({'indicator_name':[
                        '6-Month Bill Auction',
                                  '7-Year Note Auction',
                        'ADP Nonfarm Employment Change',
                                        'All Car Sales',
                                      'All Truck Sales',
                           'API Weekly Crude Oil Stock',
                   'API Weekly Cushing Crude Oil Stock',
                        'API Weekly Distillates Stocks',
                            'API Weekly Gasoline Stock',
                       'Average Hourly Earnings (MoM)',
                                'Average Weekly Hours']})
df1 = pd.DataFrame({'indicator_name':[
                        'ADP Employment Change',
                      'Advance Goods Trade Balance',
                             'Advance Retail Sales',
                      'Average Hourly Earnings MoM',
                      'Average Hourly Earnings YoY',
               'Average Weekly Hours All Employees',
                      'Avg Hourly Earning MOM Prod',
                      'Avg Hourly Earning YOY Prod',
                      'Avg Weekly Hours Production']})

df.set_index('indicator_name', drop=False, inplace=True)
df1.set_index('indicator_name', drop=False, inplace=True)
df1.columns = ['indicator_name2']
pd.concat([df, df1], axis=1).sort_index().reset_index(drop=True)

输出

     indicator_name                     indicator_name2
0                 6-Month Bill Auction                                 NaN
1                  7-Year Note Auction                                 NaN
2                                  NaN               ADP Employment Change
3        ADP Nonfarm Employment Change                                 NaN
4           API Weekly Crude Oil Stock                                 NaN
5   API Weekly Cushing Crude Oil Stock                                 NaN
6        API Weekly Distillates Stocks                                 NaN
7            API Weekly Gasoline Stock                                 NaN
8                                  NaN         Advance Goods Trade Balance
9                                  NaN                Advance Retail Sales
10                       All Car Sales                                 NaN
11                     All Truck Sales                                 NaN
12       Average Hourly Earnings (MoM)                                 NaN
13                                 NaN         Average Hourly Earnings MoM
14                                 NaN         Average Hourly Earnings YoY
15                Average Weekly Hours                                 NaN
16                                 NaN  Average Weekly Hours All Employees
17                                 NaN         Avg Hourly Earning MOM Prod
18                                 NaN         Avg Hourly Earning YOY Prod
19                                 NaN         Avg Weekly Hours Production