我正在用 Python 處理一些數據,想知道是否有人可以提供幫助。我有這樣的數據: count source timestamp tokens0 1 alt-right-census 2006-03-21 setting1 1 alt-right-census 2006-03-21 twttr2 1 stormfront 2006-06-24 head3 1 stormfront 2006-10-07 five我需要這樣的數據:count_stormfront count_alt-right-census month token2 1 2006-01 setting或者像這樣: date token alt_count storm_count4069995 2016-09 zealand 0 04069996 2016-09 zero 11 84069997 2016-09 zika 295 160我如何按年-月和樞軸匯總天數,以便 count 變成 count_source 在一個月內求和?任何幫助,將不勝感激。謝謝!
1 回答

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
df.groupby(['source', df['timestamp'].str[:7]]).size().unstack()
結果:
timestamp 2006-03 2006-06 2006-10
source
alt-right-census 2.0 NaN NaN
stormfront NaN 1.0 1.0
添加回答
舉報
0/150
提交
取消