带有整数的python中的清除百分比列(十进制+整数)

问题描述 投票:0回答:1

我有一列百分比,其中包含像0.4567 , 0.1564 , 19 , 23, 0 , 0.1234这样的数字,需要对整数进行标准化,即45 , 15, 19 ,23 , 0 , 12这样。我转载了如下示例。

import pandas as pd
import numpy as np
n_row =  10
dicti = {'id':[coli for coli in range(1,(n_row+1))],
     'perc_col':[30,0.4546,0.76543223190,10,0,0.29567,93,15,0.31,0.456]}
df = pd.DataFrame(dicti)
df

数据帧输出

enter image description here

预期输出enter image description here

python percentage
1个回答
0
投票

您可以通过这样的列表理解来做到这一点:

import pandas as pd
import numpy as np
n_row =  10
dicti = {'id':[coli for coli in range(1,(n_row+1))],
         'perc_col':[int(x) if x > 1 else int(x*100) for x in [30,0.4546,0.76543223190,10,0,0.29567,93,15,0.31,0.456] ]}
df = pd.DataFrame(dicti)
df

输出

   id  perc_col
0   1        30
1   2        45
2   3        76
3   4        10
4   5         0
5   6        29
6   7        93
7   8        15
8   9        31
9  10        45
© www.soinside.com 2019 - 2024. All rights reserved.