简体   繁体   English

Python/Pandas 替换列函数中的值

[英]Python/Pandas Replace Values in a column function

**Using Pandas 1.4.2, Python 3.9.12 **使用熊猫 1.4.2、Python 3.9.12

I have a data set where the column values are represented as 0 or 1 which stand for 'No' and 'Yes', respectively.我有一个数据集,其中列值表示为 0 或 1,分别代表“否”和“是”。

  Scholarship     Hipertension   Diabetes   Alcoholism  SMS_received    
0     0               1              0          0            0  
1     0               0              0          0            0  
2     0               0              0          0            0  
3     0               0              0          0            0  
4     0               1              1          0            0  

I am attempting to create a custom function to replace the 0's and 1's all at once with 'No' and 'Yes', respectively.我正在尝试创建一个自定义函数,分别用“否”和“是”一次性替换 0 和 1。

What I have written at this point is as follows:我在这一点上写的内容如下:

def replace_values(data_frame, column, being_replaced, replacement_value):
    data_frame[column] = df[column].replace(to_replace=being_replaced, value= 
    replacement_value)
return df

As an example, I would like to be able to put all the column names in and the values being replaced and replacement values so the function will do everything in one fell swoop.例如,我希望能够将所有列名以及要替换的值和替换值放入其中,以便该函数一举完成所有事情。 Such as:如:

replace_values(df, [*list_of_columns*], [0, 1], ['No', 'Yes'])

Is this even possible?这甚至可能吗? Do I need to put a loop in there as well?我还需要在那里放一个循环吗? I have tried it a couple times with only one column name as opposed to a list and it works, but it replaces every 0 and 1 with 'No' and 'Yes' regardless of column, which is great, but not what I am trying to do.我已经尝试了几次,只有一个列名而不是一个列表,它可以工作,但是不管列如何,它都会用“否”和“是”替换每个 0 和 1,这很好,但不是我想要的去做。 Any help is appreciated.任何帮助表示赞赏。

here is a couple of solutions.这里有几个解决方案。

to use replace:使用替换:

df.replace({1: 'Yes', 0: 'No'})

use where, which keeps the value that fulfills the condition of the first argument and changes everything else to the value of the second argument:使用 where,它保留满足第一个参数条件的值并将其他所有内容更改为第二个参数的值:

df = df.where(df == 1, 'No')
df = df.where(df == 'No', 'Yes')

use boolean masking:使用布尔掩码:

df[df == 0] = 'No'
df[df == 1] = 'Yes'

This should work for you:这应该适合你:

def replace_values(data_frame):
  return data_frame.astype(bool)

or since you want to be able to specify the column names you can try something like this:或者因为你希望能够指定列名,你可以尝试这样的事情:

def replace_values(data_frame, list_of_columns):
  for col in list_of_columns:
    data_frame[col] = data_frame[col].astype(bool)
  return data_frame

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM