gpt4 book ai didi

python - Pandas 中特定条件下的 pd.eval()

转载 作者:行者123 更新时间:2023-12-01 06:32:09 26 4
gpt4 key购买 nike

我只想 pd.eval() 只针对那些不包含“-”符号的值。

  land_area
0 969 - 2002 <-- I want to ignore this
1 80*82
2 100

代码

df['land_area'] = df['land_area'].apply(lambda x: x if x.str.contains('|'.join(['-']) else x.apply(pd.eval))

但是它不起作用。它说

AttributeError: 'str' object has no attribute 'str'

我该如何解决这个问题?

最佳答案

df.loc[~df['land_area'].str.contains("-"), 'land_area'] = df[~df['land_area'].str.contains("-")]['land_area'].apply(eval)

使用.loc屏蔽“-”,并将其设置为等于.eval答案

关于python - Pandas 中特定条件下的 pd.eval(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59847797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com