我有一个使用以下代码创建的数据透视表:
pivot_2 = pd.pivot_table(pre_post_df_2, columns = ['pre_post_nota_ind','Party'], index = ['uni_const_code'], values = ['votesh_percof_total']).reset_index()
pivot_2.rename(columns = {1:'post', 0:'pre'}, inplace = True)
#I wrote the above code because pre_post_nota_ind takes 0 which represents pre and 1 which represents post
我得到一个数据透视表(数据框类型),看起来像这样:
votesh_percof_total
pre_post_nota_ind pre post
uni_const_code IND IND NOTA
0|10|1 71.932234 53.922378 33.827091
0|10|10 46.052632 NaN 40.106277
0|10|11 45.171340 28.443649 32.434116
0|10|12 63.429439 30.115420 NaN
0|10|13 30.000000 NaN 46.598414
旁注:第三和第四列均与“后期”期间相关,因此它们具有相同的标题。
Basically, there are sub-columns in the pivot table above. I want to remove those rows for whose value in the NOTA column is Nan
我希望最终输出像:
votesh_percof_total
pre_post_nota_ind pre post
uni_const_code IND IND NOTA
0|10|1 71.932234 53.922378 33.827091
0|10|10 46.052632 NaN 40.106277
0|10|11 45.171340 28.443649 32.434116
0|10|13 30.000000 NaN 46.598414
I tried the following:
pivot_2.dropna(subset = pivot_2['NOTA'])
and also
pivot_2 = pivot_2.votesh_percof_total.post.notnull()]
他们两个都抛出错误。有人可以帮我吗?