包含所有索引的数据透视表

时间:2016-09-18 14:29:27

标签: python python-2.7 pandas pivot pivot-table

我正在使用python pandas从df创建数据透视表。 df看起来像:

enter image description here

缺少值的字段包括:Origin City,Shipment Date,Volume and Landing Date。请注意,着陆日期是装运日期和TAT的总和。

我想要输出的是:

enter image description here

上面的输出我有以下代码:

pd.pivot_table(df, values='Volume', index=['DC'], columns=['Landing date'], aggfunc=np.sum, fill_value = 0)

我得到的实际输出是

enter image description here

这里的问题是我的代码会移除DC = DLT,因为它在旋转时缺少值。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以ffill使用method='ffill'print (df) DC Landing date Volume 0 MAR 02-09-16 50.0 1 MAR 03-09-16 98.0 2 MAR NaN NaN 3 BOY 05-09-16 60.0 4 BOY 06-09-16 14.0 5 DLT NaN NaN 6 DLT NaN NaN df['Landing date'] = df['Landing date'].ffill() print (df) DC Landing date Volume 0 MAR 02-09-16 50.0 1 MAR 03-09-16 98.0 2 MAR 03-09-16 NaN 3 BOY 05-09-16 60.0 4 BOY 06-09-16 14.0 5 DLT 06-09-16 NaN 6 DLT 06-09-16 NaN df1 = pd.pivot_table(df, values='Volume', index=['DC'], columns=['Landing date'], aggfunc=np.sum, fill_value = 0) df1.index.name = None df1.columns.name = None print (df1) 02-09-16 03-09-16 05-09-16 06-09-16 BOY 0 0 60 14 DLT 0 0 0 0 MAR 50 98 0 0 Landing date相同:

val = df['Landing date'].dropna().iloc[0]
print (val)
02-09-16

df['Landing date'] = df['Landing date'].fillna(val)
print (df)
    DC Landing date  Volume
0  MAR     02-09-16    50.0
1  MAR     03-09-16    98.0
2  MAR     02-09-16     NaN
3  BOY     05-09-16    60.0
4  BOY     06-09-16    14.0
5  DLT     02-09-16     NaN
6  DLT     02-09-16     NaN

您可以Series.fillna来自 Intent i = new Intent(this.getApplicationContext(),MainActivity.class); i.putExtra((inputuserName.getText()).toString(), true); startActivity(i); 列的第一个非NaN值:

options:{
   mainConfigFile: "path/to/Config.js"
}