我正在尝试根据某些条件计算dataframe
中的值,并从我从value_counts获得的
我不知道为什么,但它有时返回 series
创建dataframe
()int
有时返回 float
...我想将所有内容标准化为 int
并在 concat
dataframe
中添加第一个空的 column
名称
data = base.get_shuukeihyou(data_from=date_from, data_to=date_to, name=name_field)
sougou_call = data['charge'].value_counts()
data_tsunagatta = data[data.status != "gen"]
data_tsunagatta = data_tsunagatta[data_tsunagatta.status != "empty"]
data_tsunagatta = data_tsunagatta['charge'].value_counts()
apo = data[data.status == "apo"]
apo = apo['charge'].value_counts()
kontakuto = data[data.contact != "info"]
kontakuto = kontakuto[kontakuto.contact != "another"]
kontakuto = kontakuto['charge'].value_counts()
alls = pd.concat([sougou_call, data_tsunagatta, apo, kontakuto], axis=1)
alls = alls.fillna(0)
alls.columns = ["one", "two", "three", "four"]
print(alls)
返回这个
one two three four
bbbb aaaa 2 0.0 0.0 0.0
john smith 1 1.0 0.0 1.0
我想要这个
name one two three four
bbbb aaaa 2 0 0 0
john smith 1 1 0 1
最佳答案
重置索引并使用新的convert_dtypes
方法。
>>> df
one two three four
bbbb aaaa 2 0.0 0.0 0.0
john smith 1 1.0 0.0 1.0
>>> df.index.name = 'name'
>>> df.reset_index().convert_dtypes()
name one two three four
0 bbbb aaaa 2 0 0 0
1 john smith 1 1 0 1
关于python - value_counts 在 DataFrame 中返回 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62510138/