首页 > 解决方案 > 循环熊猫列并替换值

问题描述

我已经阅读了很多帖子,但没有成功。我有一个我希望的列“百分比”,但在类别 1、2、3、4 中。数据框称为 'data' 。我试过了

for i in data.index:
    if i > 0.7:
        df.at[i,"percent"] =1
    if i <0.7 and i>0:
        df.at[i, "percent"] = 2
    if i <0 and i > -0.4:
        df.at[i, "percent"] = 3
    if i < 0.4:
        df.at[i, "percent"] = 4

但看起来一切都被替换为1。我做错了什么?

标签: pythonpandasloops

解决方案


import pandas as pd
import numpy as np

df = pd.DataFrame([[0.4,"x"],[0.5,"x"], [0.6,"y"], [0.7,"z"], [0.8,"z"]], columns=["pc","val"])

df['pc_quant'] =  np.digitize(df['pc'], [.4, .7])

print(df)

给你:

    pc val  pc_quant
0  0.4   x         1
1  0.5   x         1
2  0.6   y         1
3  0.7   z         2
4  0.8   z         2

推荐阅读