数据帧中返回浮点数的值

2024-09-27 21:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据一些条件计算dataframe中的值,并从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


Tags: namedataframedatavaluestatusonethreefour
1条回答
网友
1楼 · 发布于 2024-09-27 21:32:50

重置索引并使用新的^{}方法

>>> 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

相关问题 更多 >

    热门问题