python - Python dataframe; trouble changing value of column with multiple filters
问题描述
I have a large dataframe I took off an ODBC database. The Dataframe has multiple columns; I'm trying to change the values of one column by filtering two other. First, I filter my dataframe data_prem with both conditions which gives me the correct rows:
data_prem[(data_prem['PRODUCT_NAME']=='ŽZ08') & (data_prem['BENEFIT'].str.contains('19.08.16'))]
Then I use the replace function on the selection to change 'M' value to 'H' value:
data_prem[(data_prem['PRODUCT_NAME']=='ŽZ08') & (data_prem['BENEFIT'].str.contains('19.08.16'))]['Reinsurer'].replace(to_replace='M',value='H',inplace=True,regex=True)
Python warns me I'm trying to modify a copy of the dataframe, even though I'm clearly refering to the original dataframe (I'm posting image so you can see my results).
I also tried using .loc function in the following manner:
data_prem.loc[((data_prem['PRODUCT_NAME']=='ŽZ08') & (data_prem['BENEFIT'].str.contains('19.08.16'))),'Reinsurer'] = 'H'
which changed all rows that fit the second condition (str.contains...), but it didn't apply the first condition. I got replacements in the 'Reinsurer' column for other 'PRODUCT_NAME' values as well.
I've been scouring the web for an answer to this for some time. I've seen some mentions of a bug in the pandas library, not sure if this is what they were talking about.
I would value any opinions you might have, would also be interesting in alternative ways to solving this problem. I filled the 'Reinsurer' column with the map function with 'PRODUCT_NAME' as the input (had a dictionary that connected all 'PRODUCT_NAME' values with 'Reinsurer' values).
解决方案
给定您的 Boolean mask
,您已经演示了两种应用链式索引的方法。这是警告的原因,也是您没有看到您的逻辑按预期应用的原因。
mask = (data_prem['PRODUCT_NAME']=='ŽZ08') & df['BENEFIT'].str.contains('19.08.16')
链式索引:示例 #1
df[mask]['Reinsurer'].replace(to_replace='M', value='H', inplace=True, regex=True)
链式索引:示例 #2
df[mask].loc[mask, 'Reinsurer'] = 'H'
避免链式索引
您可以通过应用mask
一次并使用单个loc
调用来保持简单:
df.loc[mask, 'Reinsurer'] = 'H'
推荐阅读
- php - Laravel laravel-users 向 CRUD 表单添加字段
- javascript - “未捕获的类型错误:
不是函数”用过一次后 - c# - c#中的TreeView和listview
- java - 如何以 2019 年 2 月 24 日格式在 Java 中查找未来日期(比如从今天开始的两个月)
- javascript - JavascriptExecutor 在 DOM 中设置属性函数更新,但在 GUI 中不更新
- angular - 无法在 useValue 中分配 const 值
- javascript - How to import local model into Magenta MusicRNN?
- hadoop - osx 获取 pyarrow.lib.ArrowIOError:无法加载 libhdfs
- marklogic - 尽管在 8001 和 8002 上成功,但无法连接到查询控制台 (8000)
- python - python扩展中的打印格式