【问题标题】:Apply two different formulas based on the cell content of a dataframe根据数据框的单元格内容应用两个不同的公式
【发布时间】:2022-01-19 23:14:34
【问题描述】:

我有这个名为 optiondf 的数据框:

     strike  type settle openInterest volume
0     10000  Call  24'24        0      0
1     15000  Call  0            0      0
2     20000  Call  23'56        0      0
3     25000  Call  1            0      0
4     30000  Call  23'24        0      0

我希望能够对“结算”列进行一些操作。 我想根据列的值中是否存在符号“'”进行过滤。

如果包含“'”,则将此计算作为单元格的值,我已经验证过,计算本身没有问题

pd.to_numeric(optiondf['settle'].str.split(pat="'").str[0])+pd.to_numeric(optiondf['settle'].str.split(pat="'").str[-1])*(1/64)

如果不包含“'”,我只想将值转换为数字,如下所示:

optiondf['settle']=pd.to_numeric(optiondf['settle'])

我已经尝试过这个 pandas 功能,但没有成功,我可能做错了什么:

optiondf['settle'].str.contains("'")=....

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以构造一个适用于不同函数的 lambda:

    def func_a(x):
        v = x.split("'")
        return v[0] + v[1] / 64
    
    optiondf['settle'] = optiondf['settle'].apply(lambda x: func_a(x) if "'" in x else pd.to_numeric(x))
    

    【讨论】:

    • 在行上 "optiondf['settle'] = optiondf['settle'].apply(lambda x: func_a(x) if x.contains("'") else pd.to_numeric(x) )" 我得到这个错误 "'str' object has no attribute 'contains'"
    • 抱歉,我混淆了我的语言。固定
    【解决方案2】:

    您可以尝试使用apply()

    应该是这样的:

    def some_function(row):
       value = row["settle"]
       if "'" in row["settle"]:
           row["settle"] = pd.to_numeric(row['settle'].str.split(pat="'").str[0])+pd.to_numeric(row['settle'].str.split(pat="'").str[-1])*(1/64)
       else:
           row["settle"] = pd.to_numeric(row['settle'])
       return row
    
    
    updated_df = optiondf.apply(somefunction, axis=1)
    

    【讨论】:

    • row["settle"] = pd.to_numeric(optiondf['settle']) 会将特定单元格的值设置为整列的值吗?不应该是:row["settle"] = pd.to_numeric(row['settle'])
    • 是的,你是对的!来自我的错误复制/粘贴。
    【解决方案3】:

    您可以使用str.contains 创建一个布尔掩码,并使用它来过滤存在' 的行并将您的计算应用于它们,最后将整个列转换为数字类型。

    msk = df['settle'].str.contains("'")
    temp = df.loc[msk, 'settle'].str.split("'")
    df.loc[msk, 'settle'] = pd.to_numeric(temp.str[0]) + pd.to_numeric(temp.str[-1])*(1/64)
    df['settle'] = pd.to_numeric(df['settle'])
    

    或使用np.where 代替上面代码中的最后两行。 np.where 让我们根据条件分配不同的值(在我们的例子中是字符串是否包含')。如果为真,则操纵数字,否则什么也不做。然后将整列转换为数值类型。

    msk = df['settle'].str.contains("'")
    temp = df.loc[msk, 'settle'].str.split("'")
    df['settle'] = pd.to_numeric(np.where(msk, pd.to_numeric(temp.str[0]) + pd.to_numeric(temp.str[-1])*(1/64), df['settle']))
    

    输出:

       strike  type  settle  openInterest  volume
    0   10000  Call  24.375             0       0
    1   15000  Call   0.000             0       0
    2   20000  Call  23.875             0       0
    3   25000  Call   1.000             0       0
    4   30000  Call  23.375             0       0
    

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多