Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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 函数的输入对象将被return覆盖_Python_Function_Pandas_Return - Fatal编程技术网

Python 函数的输入对象将被return覆盖

Python 函数的输入对象将被return覆盖,python,function,pandas,return,Python,Function,Pandas,Return,我对Python比较陌生,我对函数有一个问题。基本上,这个函数应该编辑一个数据帧并以一个新的对象名返回它。因此,保持原始对象不变。返回的对象应保存在新对象中。我的问题是,输入对象也会受到影响。如何防止输入对象受函数影响,即使将结果保存到新对象。我读了一些关于不可变和可变对象的书,但是我该如何改变我的函数呢 import pandas as pd import numpy as np msg_items = pd.DataFrame({'Column_A': [10,20,30,40,50,60

我对Python比较陌生,我对函数有一个问题。基本上,这个函数应该编辑一个数据帧并以一个新的对象名返回它。因此,保持原始对象不变。返回的对象应保存在新对象中。我的问题是,输入对象也会受到影响。如何防止输入对象受函数影响,即使将结果保存到新对象。我读了一些关于不可变和可变对象的书,但是我该如何改变我的函数呢

import pandas as pd
import numpy as np

msg_items = pd.DataFrame({'Column_A': [10,20,30,40,50,60]})

print('Before: ', msg_items.dtypes)

def justdoit(myframe):
    cols = list(myframe)
    myframe[cols] = myframe[cols].applymap(np.str)
    return myframe

testframe = justdoit(msg_items)

print('After: ', msg_items.dtypes)
实际产量:

Before:  Column_A    int64
dtype: object
After:  Column_A    object
dtype: object
预期产出:

Before:  Column_A    int64
dtype: object
After:  Column_A    int64
dtype: object

在函数中-使用
copy.deepcopy(x)
创建一个新对象,然后更改其值并返回它

def justdoit(myframe):
    cols = list(myframe)
    myframe_new = copy.deepcopy(myframe)
    myframe_new [cols] = myframe[cols].applymap(np.str)
    return myframe_new 

如果您的目标是在不修改原始数据帧的情况下将数据帧转换为字符串,那么您做得不对,因为

myframe[cols] = myframe[cols].applymap(np.str)
  • 将结果分配回原始数据帧,以便在原地进行更改
  • 这不是将列分级为字符串的最泛化的方式吗
  • 此外

    return myframe
    
    返回您刚刚分配给新变量的同一对象。只有一个数据帧,但有两种通过两个变量访问它的方法

    要将数据帧转换为字符串,请使用
    astype(str)
    -

    def just_do_it(df):
        return df.astype(str)
    
    def just_do_it(df, subset):
        df_new = df.copy()
        df_new[subset] = df_new[subset].astype(str)
    
        return df_new
    
    new_msg_items = just_do_it(msg_items, subset=list(msg_items))
    
    df_new = df.copy(deep=True)
    
    如果要编辑子集并返回副本,请调用
    df.copy
    -

    def just_do_it(df):
        return df.astype(str)
    
    def just_do_it(df, subset):
        df_new = df.copy()
        df_new[subset] = df_new[subset].astype(str)
    
        return df_new
    
    new_msg_items = just_do_it(msg_items, subset=list(msg_items))
    
    df_new = df.copy(deep=True)
    
    作为补充说明,如果您有可变类型的列,您可能需要执行deepcopy。在这种情况下,使用
    deep=True调用
    copy
    -

    def just_do_it(df):
        return df.astype(str)
    
    def just_do_it(df, subset):
        df_new = df.copy()
        df_new[subset] = df_new[subset].astype(str)
    
        return df_new
    
    new_msg_items = just_do_it(msg_items, subset=list(msg_items))
    
    df_new = df.copy(deep=True)
    

    myframe[cols]=myframe[cols].applymap(np.str)
    编辑原始数据帧,它不会创建新的数据帧。我如何创建一个新的数据帧,而不是编辑原始数据帧?不要使用
    applymap
    ,使用
    copy
    创建一个新的数据帧(参见我的答案)。与另一个答案相同,
    applymap
    不是用于字符串转换的好方法。事实上,您甚至可以使用
    copy.deepcopy
    ,而您可以使用
    df.copy
    。谢谢您的解释!你们太棒了。没想到会有这么复杂的案子。我来自R,我倾向于只使用箭头来复制对象。@ErikSteiner干杯,这是所有python新手的通病。