我正在使用python pandas从df创建数据透视表。 df看起来像:
缺少值的字段包括:Origin City,Shipment Date,Volume and Landing Date。请注意,着陆日期是装运日期和TAT的总和。
我想要输出的是:
上面的输出我有以下代码:
pd.pivot_table(df, values='Volume', index=['DC'], columns=['Landing date'], aggfunc=np.sum, fill_value = 0)
我得到的实际输出是
这里的问题是我的代码会移除DC = DLT
,因为它在旋转时缺少值。有什么想法吗?
答案 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"
}