Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用.replace减少命令以及create函数中的问题_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何使用.replace减少命令以及create函数中的问题

Python 如何使用.replace减少命令以及create函数中的问题,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧,我想在其中用我选择的某些列中的“是”和“否”替换0,1编码。某些df列具有这种编码,因此我编写了以下命令: dados_trabalho = dados_trabalho.replace({"ASSINTOM": {0: "Sim", 1 : "Não"}}).replace({"DOR ATIPICA": {0: "Sim", 1 : "Não"}}).replace({"IAM": {0: "Sim", 1 : "Não"}}).replace({"HAS": {0: "Si

我有一个数据帧,我想在其中用我选择的某些列中的“是”和“否”替换0,1编码。某些df列具有这种编码,因此我编写了以下命令:

dados_trabalho = dados_trabalho.replace({"ASSINTOM": {0: "Sim", 1 : "Não"}}).replace({"DOR ATIPICA": {0: "Sim", 1 : "Não"}}).replace({"IAM": {0: "Sim", 1 : "Não"}}).replace({"HAS": {0: "Sim", 1 : "Não"}}).replace({"DM": {0: "Sim", 1 : "Não"}}).replace({"DISPLIP": {0: "Sim", 1 : "Não"}}).replace({"DOR TIPICA": {0: "Sim", 1 : "Não"}})
它正确运行并替换了新编码标识的列,但我想知道是否有方法总结这个公式,以便脚本不会变得庞大

我尝试创建函数:

def change_columns (df):
    c = df.columns
    df = df.replace ({c: {0: "Yes", 1: "No"}})
问题是,当我在此函数中输入数据帧时,会发生以下错误:

change_columns (df)

TypeError Traceback (most recent call last)
<ipython-input-141-43eb9316b19b> in <module>
----> 1 change_columns (df)

<ipython-input-140-9fbbd4e9e293> in change_columns (df)
      1 def change_columns (df):
      2 c = df.columns
----> 3 df = df.replace ({c: {0: "Yes", 1: "No"}})

/usr/lib/python3/dist-packages/pandas/core/indexes/base.py in __hash __ (self)
   2060
   2061 def __hash __ (self):
-> 2062 raise TypeError ("unhashable type:% r"% type (self) .__ name__)
   2063
   2064 def __setitem __ (self, key, value):

TypeError: unhashable type: 'Index'
您创建的函数(
change\u columns(df)
)似乎正在尝试对所有列执行替换。如果这是您的意图,那么您不需要任何特殊的函数或链式方法调用。您所需要的只是:

dados_trabalho = dados_trabalho.replace({0: "Sim", 1 : "Não"})
为了只替换某些列中的0和1,您需要告诉函数要对哪些列执行替换。例如:

import pandas

def change_columns(df, cols):
    for col_name in cols:
        df = df.replace({col_name: {0:'yes', 1:'no'}})
    return df

# create sample data
df = pandas.DataFrame([[0, 0, 1, 0, 1, 1], [1, 0, 1, 0, 1, 0]])
print('Starting DataFrame:')
print(df)

# define columns to do the replacement
columns_to_replace = [0, 2, 3]
# perform the replacement
df = change_columns(df, columns_to_replace)

# see the result
print('After processing DataFrame: ')
print(df)
运行上述代码应产生以下结果:

Starting DataFrame:
   0  1  2  3  4  5
0  0  0  1  0  1  1
1  1  0  1  0  1  0
After processing DataFrame:
     0  1   2    3  4  5
0  yes  0  no  yes  1  1
1   no  0  no  yes  1  0
Starting DataFrame:
   0  1  2  3  4  5
0  0  0  1  0  1  1
1  1  0  1  0  1  0
After processing DataFrame:
     0  1   2    3  4  5
0  yes  0  no  yes  1  1
1   no  0  no  yes  1  0