简体   繁体   English

根据条件替换列中的值,然后返回数据框

[英]Replace values in column based on condition, then return dataframe

I'd like to replace some values in the first row of a dataframe by a dummy. 我想用假人替换数据框第一行中的某些值。

df[[0]].replace(["x"], ["dummy"])

The problem here is that the values in the first column are replaced, but not as part of the dataframe. 这里的问题是替换了第一列中的值,但没有将其作为数据框的一部分。

print(df)

yields the dataframe with the original data in column 1. I've tried 产生具有第1列中原始数据的数据框。我尝试过

df[(df[[0]].replace(["x"], ["dummy"]))]

which doesn't work either.. 这也不起作用..

replace returns a copy of the data by default, so you need to either overwrite the df by self-assign or pass inplace=True : 默认情况下, replace返回数据的副本,因此您需要通过自分配覆盖df或传递inplace=True

df[[0]].replace(["x"], ["dummy"], inplace=True)

or 要么

df[0] = df[[0]].replace(["x"], ["dummy"])

see the docs 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据条件替换数据框列中的值 - Replace values in a dataframe column based on condition 如何根据第二个 Dataframe 值的条件替换 Dataframe 列值 - How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values 如何根据具有一系列值的条件替换 pd 数据框列中的值? - How to Replace values in a pd dataframe column based on a condition with a range of values? 根据条件从另一个 dataframe 值替换列的值 - Python - Replace values of a column from another dataframe values based on a condition - Python 根据条件,用相应的列名替换 pandas 数据框中的特定值, - Replace specific values in pandas dataframe with the corresponding column name, based on a condition, 如何根据条件用NaN替换数据框列值? - How to replace a dataframe column values with NaN based on a condition? 根据条件使用字典替换 python dataframe 中的列值 - Replace column values in python dataframe using dictionary based on condition 根据条件用不同的替换字典替换熊猫数据框列中的值 - Replace values in pandas dataframe column with different replacement dict based on condition Pandas DataFrame:根据条件替换列中的所有值 - Pandas DataFrame: replace all values in a column, based on condition 根据条件返回 dataframe 的特定值 - Return specific values of a dataframe based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM