python - python iloc错误新列数据框
问题描述
我有这个 python 脚本,这只是它的一部分,它可以工作,但只有 2 行我遇到了麻烦:
not_marketo.loc['Marketo DEP'] = "NO"
yes_marketo.loc[:,'Marketo DEP'] = C
我已经尝试了所有可能的方法:
not_marketo['Marketo DEP'] = "NO"
not_marketo.loc['Marketo DEP'] = "NO"
not_marketo.loc[:,'Marketo DEP'] = "NO"
“试图在 DataFrame 的切片副本上设置一个值。尝试改用 .loc[row_indexer,col_indexer] = value”
SettingWithCopyWarning:
pandas\core\indexing.py:1596:SettingWithCopyWarning:试图在 DataFrame 中的切片副本上设置值请参阅文档中的警告: https ://pandas.pydata.org/pandas-docs /stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value, self.name)
# Entry Point List (epl)
df_left = df_db[['Email', 'SOURCE', 'Data Entry Point', 'File']]
df_right = df_mto[['Email', 'Entry Point List', 'Marketo']]
df_epl = pd.merge(df_left, df_right, on="Email", how = 'outer')
df_epl.loc[df_epl['Marketo'].isnull(), 'Marketo'] = 'NO'
df_epl.loc[:,'Entry Point List'] = df_epl['Entry Point List'].str.replace('|',' ', regex=True)
# List of Data Entry Points from the Files
dep_list = df_epl[['Data Entry Point']]
dep_list = dep_list.dropna()
dep_list = dep_list.drop_duplicates()
list = dep_list['Data Entry Point'].tolist()
# By Groups
yes_marketo = df_epl[(df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())]
not_marketo = df_epl[(df_epl['Marketo'] == "NO") & (df_epl['File'].notnull())]
not_files = df_epl.loc[df_epl['File'].isnull()]
# If not in Marketo not Entry Data Point
not_marketo.loc['Marketo DEP'] = "NO"
# Check Entry Point List for yes_marketo
C = []
for index, row in yes_marketo.iterrows():
if row['Data Entry Point'] in row['Entry Point List']:
C.append('YES')
else:
C.append('NO')
yes_marketo.loc[:,'Marketo DEP'] = C
解决方案
您在显示的代码中多次不安全地对 DataFrame 进行了子集化:
# UNSAFE SUBSET HERE
dep_list = df_epl[['Data Entry Point']]
# UNSAFE SUBSET HERE
yes_marketo = df_epl[(df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())]
# UNSAFE SUBSET HERE
not_marketo = df_epl[(df_epl['Marketo'] == "NO") & (df_epl['File'].notnull())]
# UNSAFE SUBSET HERE
not_files = df_epl.loc[df_epl['File'].isnull()]
可能最简单的修复方法是添加copy
dep_list = df_epl[['Data Entry Point']].copy()
yes_marketo = df_epl[
(df_epl['Marketo'] == "YES") & (df_epl['File'].notnull())
].copy()
not_marketo = df_epl[
(df_epl['Marketo'] == "NO") & (df_epl['File'].notnull())
].copy()
not_files = df_epl.loc[df_epl['File'].isnull()].copy()
推荐阅读
- php - 解密 Laravel 加密值
- java - 从数组列表中返回第一个索引,其中发现字符串逻辑混乱
- excel - VBA/宏:如何拆分 excel 工作表并使用不同的密码保护每张工作表?
- sql - XML 将 txt 文件导入到 SQL。重复的 ID 错误消息?
- python - 在python(tkinter)的前端显示时如何删除数据库数据格式?
- vega-lite - 根据颜色条件更新 Vega-lite 图例颜色
- java - 带有嵌套循环的 Java 10x10 数组
- javascript - 使用 IntersectionObserver 和高密度像素替代方案延迟加载图像。如何在同一图像中使用这两个功能?
- java - Spring Boot OAuth2 管道
- typescript - io-ts 的字段名称/密钥验证?