代码之家  ›  专栏  ›  技术社区  ›  Anti21

如何在python中使用dataframe传递变量

  •  0
  • Anti21  · 技术社区  · 6 年前

    我有一个这样的数据集:

    Id  1456    1457    1458    1459    1460
    MSSubClass  60  20  70  20  20
    MSZoning    RL  RL  RL  RL  RL
    LotFrontage 62  85  66  68  75
    LotArea 7917    13175   9042    9717    9937
    Street  Pave    Pave    Pave    Pave    Pave
    Alley   NaN NaN NaN NaN NaN
    LotShape    Reg Reg Reg Reg Reg
    LandContour Lvl Lvl Lvl Lvl Lvl
    

    我把琴弦改成了熊猫。现在我需要把它们转换成数字数据。为了将其转换为数值数据,我采用以下输出:

    display_all(df_raw.isnull().sum().sort_index()/len(df_raw))
    1stFlrSF         0.000000
    2ndFlrSF         0.000000
    3SsnPorch        0.000000
    Alley            0.937671
    BedroomAbvGr     0.000000
    BldgType         0.000000
    BsmtCond         0.025342
    BsmtExposure     0.026027
    BsmtFinSF1       0.000000
    

    如果值不为零,我就把它转换成数值。

    train_cats(df_raw) #convert strings to pandas
    op1=df_raw.isnull().sum().sort_index()/len(df_raw)
    i=0
    while i < op1.shape[0]:
        if op1[i]!=0.0:
            variabe_name=op1.index[i]
            df_raw.variable_name = df_raw.variable_name.cat.codes <----
        i+=1
    

    所以在Alley不为零时的原始值为: 测向_原始胡同=测向_生。胡同。猫.代码

    我的问题是如何传递变量名而不是值,这样我就可以循环使用它了?我试过@variable_name,但它只会给我错误/

    也许我做错了。有更好的方法吗?

    非常感谢你的帮助。

    0 回复  |  直到 6 年前