Mar*_*ari 0 python python-3.x pandas
我有一个像下面这样的 int dtype 的 df 。我想在 pandas df 列中的每个值的左侧两位数字后添加小数点
Descrip a b
VP3 52366599 10718233
VP3 522842650 106751
.
.
VP4 5232937 10542931
VP5 522842650 10615982
.
.
Run Code Online (Sandbox Code Playgroud)
我希望我的 Df 是这样的
Descrip a b
VP3 52.366599 10.718233
VP3 52.2842650 10.6751
.
.
VP4 52.32937 10.542931
VP5 52.2842650 10.615982
.
.
Run Code Online (Sandbox Code Playgroud)
由于数据框中的值没有相同的位数,所以我无法通过将每个数字除以 10e(某物)来处理简单的方法
我希望 pandas 有一个简单的方法来解决这个问题
您可以使用迭代列并在所需位置str插入:.
df = pd.DataFrame(np.random.randint(0, 2000, (5, 2)))
print(df)
0 1
0 97 148
1 796 935
2 1992 594
3 1498 416
4 34 1289
df = df.astype(str)
for c in df:
df[c] = (df[c].str[:2] + '.' + df[c].str[2:]).astype(float)
print(df)
0 1
0 97.00 14.80
1 79.60 93.50
2 19.92 59.40
3 14.98 41.60
4 34.00 12.89
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4301 次 |
| 最近记录: |