我想要一个数据框恰好有这些列:
columns=['index', 'count_1','count_2', 'count_3', 'count_4', 'count_5']
因此,如果给定的数据框没有,例如'count_2',我想将count_2
添加到所有值为0的数据框中
如果这个数据框有其他列不在columns
列表中,我想删除整个列。
的例子:输入dataframe:
'index' 'count_1','count_2' 'count_3' 'count_4' 'count_5'
1 10 12 45 0 23
是好的!
dataframe:
'index' 'count_1', 'count_3' 'count_4' 'count_5'
1 10 45 0 23
应该转换为:
'index' 'count_1','count_2' 'count_3' 'count_4' 'count_5'
1 10 0 45 0 23
dataframe
'index' 'count_1','count_2' 'count_3' 'count_4' 'count_5' 'count_6'
1 10 0 45 0 23 11
应该转换为:
index' 'count_1','count_2' 'count_3' 'count_4' 'count_5'
1 10 0 45 0 23
每列使用DataFrame.reindex
,将缺失的值替换为0
:
columns=['index', 'count_1','count_2', 'count_3', 'count_4', 'count_5']
df = df.reindex(columns=columns, fill_value=0)