python - Pandas/Python 循环遍历数据框的行并根据包含 NA'a 的另一列值更改一列
问题描述
我有一个大数据框,我需要循环并根据另一列的相同行值更改一个行值。我已经阅读了这篇文章Pandas/Python: Set value of one column based on value in another column和这篇博文https://towardsdatascience.com/a-really-simple-way-to-edit-row-by-row- in-a-pandas-dataframe-75d339cbd313但这种方法对我不起作用。我认为出错的是我从 (label2) 索引的列在某些行中包含空值。
for index in df.index:
if df.loc[index,'label2']=='Some label to match':df.loc[index,'label1'] = 'label to change'
有没有办法在熊猫中解决这个问题,例如,在循环中使用 df.notnull() ?我尝试了以下方法,但它不起作用:
df['label2'].notnull(df.loc[index,'label2']=='Some label to match':df.loc[index,'label1'] = 'label to change')
提前致谢!
解决方案
推荐阅读
- javascript - 在对象的父数组中查找唯一对象
- c++ - std::optional 如何在幕后工作?
- mysql - Java JPA:无法找到 2 个 LocalDates 之间的所有记录
- pytorch - CoreML 中的潜在向量方差比 PyTorch 大得多
- shell - zsh `getopts` OPTIND 行为与其他 shell(bash、sh)不一致
- c# - 计时器会话结束时使表单上的按钮不可见
- python - 初学者python和montecarlo
- php - 在基于 Zend Framework 的站点中重定向单个 url
- python-3.x - 为什么我无法在 Pandas 中使用 na_values?
- azure - 如何在 Azure 资源管理器模板中为 Azure 函数配置应用服务托管证书?