简体   繁体   English

如何在 function 中操作 DataFrame 名称?

[英]How can I manipulate a DataFrame name within a function?

How can I manipulate a DataFrame name within a function so that I can have a new DataFrame with a new name that is derived from the input DataFrame name in return? How can I manipulate a DataFrame name within a function so that I can have a new DataFrame with a new name that is derived from the input DataFrame name in return?

let say I have this:假设我有这个:

def some_func(df):
   # some operations
   return(df_copy)

and whatever df I put inside this function it should return the new df as..._copy, eg some_func(my_frame) should return my_frame_copy.无论我在这个 function 中放入什么 df,它都应该将新的 df 作为 ..._copy 返回,例如 some_func(my_frame) 应该返回 my_frame_copy。

Things that I considered are as follows:考虑的事情如下:

  1. As in string operations;与字符串操作一样; new_df_name = "{}_copy".format(df) -- I know this will not work since the df refers to an object but it just helps to explain what I am trying to do. new_df_name = "{}_copy".format(df) -- 我知道这不起作用,因为 df 指的是 object 但它只是有助于解释我想要做什么。
def date_timer(df):
    df_copy = df.copy()
    dates = df_copy.columns[df_copy.columns.str.contains('date')]
    for i in range(len(dates)):
        df_copy[dates[i]] = pd.to_datetime(df_copy[dates[i]].str.replace('T', ' '), errors='coerce')
    return(df_copy) 
  1. Actually this was the first thing that I tried, If only DataFrame had a "name" attribute which allowed us to manipulate the name but this also not there:实际上这是我尝试的第一件事,如果只有 DataFrame 有一个“名称”属性,它允许我们操纵名称,但这也不存在:
df.name

Maybe f-string or any kind of string operations could be able to make it happen.也许 f-string 或任何类型的字符串操作都可以实现它。 if not, it might not be possible to do in python.如果没有,则可能无法在 python 中进行。

I think this might be related to variable name assignment rules in python.我认为这可能与 python 中的变量名分配规则有关。 And in a sense what I want is reverse engineer that but probably not possible.从某种意义上说,我想要的是逆向工程,但可能是不可能的。 Please advice...请指教...

It looks like you're trying to access / dynamically set the global/local namespace of a variable from your program.看起来您正在尝试从程序中访问/动态设置变量的全局/局部命名空间。

Unless your data object belongs to a more structured namespace object , I'd discourage you from dynamically setting names with such a method since a lot can go wrong, as per the docs :除非您的数据 object 属于更结构化的命名空间 object ,否则我不鼓励您使用这种方法动态设置名称,因为很多 go 错误,根据文档

Changes may not affect the values of local and free variables used by the interpreter.更改可能不会影响解释器使用的局部变量和自由变量的值。


The name attribute of your df is not an ideal solution since the state of that attribute will not be set on default.您的dfname属性不是一个理想的解决方案,因为默认情况下不会设置该属性的 state。 Nor is it particularly common.也不是特别常见。 However, here is a solid SO answer which addresses this.但是,这是一个可靠的SO 答案,可以解决这个问题。


You might be better off storing your data objects in a dictionary, using dates or something meaningful as keys.您最好将数据对象存储在字典中,使用日期或有意义的东西作为键。 Example:例子:

my_data = {}

for my_date in dates:
    df_temp = df.copy(deep=True) # deep copy ensures no changes are translated to the parent object
    # Modify your df here (not sure what you are trying to do exactly
    df_temp[my_date] = "foo"
    
    # Now save that df
    my_data[my_date] = df_temp

Hope this answers your Q. Feel free to clarify in the comments.希望这能回答您的问题。请随时在评论中澄清。

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

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