2017-02-15 173 views
-1

我有以下DataFrame。描述城市裏,每一個用戶生活按數據分組到數據框列與熊貓

 City  Name Date 
0 Seattle Alice 2017 
1 Seattle  Bob 2011 
2 Portland Mallory 2010 
3 Seattle Mallory 2016 
4 Memphis  Bob 2012 
5 Portland Mallory 2013 

您大熊貓可以實現以下?

 Name  City1 Date1 City2 Date2 City3 Date3 
0 Alice  Seattle 2017 NaN  NaN  NaN  NaN 
1 Bob  Seattle 2011 Memphis 2012 NaN  NaN 
2 Mallory Portland 2010 Seattle 2016 Portland 2013 

非常感謝!

回答

0

您可以使用groupby自定義函數,其中創建新DataFrame,然後unstack,通過sort_index和最後使用joinMultiIndex排序第二級爲將其刪除:

df1 = df.groupby('Name')['City','Date'] 
     .apply(lambda x: pd.DataFrame(x.values, 
             columns = ['City','Date'], 
             index = np.arange(1, len(x) + 1).astype(str))) 
     .unstack() 
df1 = df1.sort_index(axis=1, level=1).replace({None:np.nan}) 
df1.columns = df1.columns.map(''.join) 
print (df1) 
      City1 Date1 City2 Date2  City3 Date3 
Name              
Alice  Seattle 2017  NaN  NaN  NaN  NaN 
Bob  Seattle 2011 Memphis 2012.0  NaN  NaN 
Mallory Portland 2010 Seattle 2016.0 Portland 2013.0