2016-07-11 93 views
2

我有兩個數據幀組成一個類似的信息類型。我試圖合併它們並重新組織它們。這裏是dataframes樣本:熊貓使用sort_values排序2數據框然後按日期排序

df1 = 
Member Nbr Name-First  Name-Last  Date-Join 
20     Zoe  Soumas  2011-08-01 
3128    Julien  Bougie  2011-07-22 
3535    Michel  Bibeau  2015-02-18 
4116   Christopher  Duthie  2014-12-02 
4700    Manoj  Chauhan  2014-11-11 
4802     Anna  Balian  2014-07-26 
5004    Abdullah   Cekic  2012-03-12 
5130    Raymonde  Girard  2011-01-04 



df2 =  
Member Nbr Name-First  Name-Last  Date-Join 
3762    Robert  Ortopan  2010-01-31 
3762    Robert  Ortopan  2010-02-28 
3892   Christian   Burnet  2010-03-24 
3892   Christian   Burnet  2010-04-24 
5022    Robert  Ngabirano  2010-06-25 
5022    Robert  Ngabirano  2010-07-28 

什麼,我想有是由Member Nbr,如果會員註冊日期多次出現那麼它將orgonized更加其中再一次排序的數據幀。因此,我將有:

df12 = 
Member Nbr Name-First  Name-Last  Date-Join 
20     Zoe   Soumas  2011-08-01 
3128    Julien   Bougie  2011-07-22 
3535    Michel   Bibeau  2015-02-18 
3762    Robert  Ortopan  2010-01-31 
3762    Robert  Ortopan  2010-02-28 
3892   Christian   Burnet  2010-03-24 
3892   Christian   Burnet  2010-04-24  
4116   Christopher   Duthie  2014-12-02 
4700    Manoj  Chauhan  2014-11-11 
4802    Anna   Balian  2014-07-26 
5004   Abdullah   Cekic  2012-03-12 
5022    Robert  Ngabirano  2010-06-25 
5022    Robert  Ngabirano  2010-07-28  
5130   Raymonde   Girard  2011-01-04 

我設法concatonate在df1底部哪個地方df2使用df12 = pd.concat([df1, df2], ignore_index=True)兩個數據幀。使用後

df12.sort_values(by='Member Nbr', axis=0, inplace=True) 

成員按照升序排列,但那些出現不止一次(在不同的連接日期)的成員按降序排列。那就是

Member Nbr Name-First  Name-Last  Date-Join 
20     Zoe   Soumas  2011-08-01 
3128    Julien   Bougie  2011-07-22 
3535    Michel   Bibeau  2015-02-18 
3762    Robert  Ortopan  2010-02-28 # Wrongly sorted 
3762    Robert  Ortopan  2010-01-31 
3892   Christian   Burnet  2010-04-24 # Wrongly sorted 
3892   Christian   Burnet  2010-03-24  
4116   Christopher   Duthie  2014-12-02 
4700    Manoj  Chauhan  2014-11-11 
4802    Anna   Balian  2014-07-26 
5004   Abdullah   Cekic  2012-03-12 
5022    Robert  Ngabirano  2010-07-28 # Wrongly sorted 
5022    Robert  Ngabirano  2010-06-25  
5130   Raymonde   Girard  2011-01-04 

有沒有辦法讓那些有多個加入日期的成員按日期升序排列?

回答

1

by參數可以是列的列表,以使數據幀是由第一列第一排序(以及用於通過第二列關係,以及用於由第三列等的關係)

df12.sort_values(by=['Member Nbr', 'Date-Join'], inplace=True) 

產生

Member Nbr Name-First Name-Last Date-Join 
0   20   Zoe  Soumas 2011-08-01 
1   3128  Julien  Bougie 2011-07-22 
2   3535  Michel  Bibeau 2015-02-18 
4   3762  Robert Ortopan 2010-01-31 
3   3762  Robert Ortopan 2010-02-28 
6   3892 Christian  Burnet 2010-03-24 
5   3892 Christian  Burnet 2010-04-24 
7   4116 Christopher  Duthie 2014-12-02 
8   4700  Manoj Chauhan 2014-11-11 
9   4802   Anna  Balian 2014-07-26 
10  5004  Abdullah  Cekic 2012-03-12 
12  5022  Robert Ngabirano 2010-06-25 
11  5022  Robert Ngabirano 2010-07-28 
13  5130  Raymonde  Girard 2011-01-04 

請注意,爲使此功能正常工作,Date-Join列應爲datetime類型。