简体   繁体   English

将字符串作为数据帧访问并在 python 循环中迭代地重新定义 object

[英]Access strings as data frames and redefine the object iteratively in python loop

for i in dataframe_list:
    i=eval(i)
    for num in range(1,len(dataframe_list)):
       
       for column in [column for column in eval(i).columns if column not in eval(dataframe[num]):
             eval(i)= eval(i).withcolumn(column, lit=none)
       for column in [column for column in dataframe.columns if column not in dataframe2]:
             eval(dataframe[num])= eval(dataframe[num]).withcolumn(column,lit=none)

Dataframe_list is a list of the names of the dataframes. Dataframe_list 是数据帧名称的列表。 The problem is python doesn't recognise the strings as the object.问题是 python 无法将字符串识别为 object。 So I use the eval() function.所以我使用 eval() function。 On the first with column containing the eval(i).withcolumn statement, I get an error saying saying eval must contain a string, bytecode or code object.在包含 eval(i).withcolumn 语句的第一个 with 列中,我收到一条错误消息,指出 eval 必须包含字符串、字节码或代码 object。 To my knowledge i is the index of the list and is clearly a string.据我所知,我是列表的索引,显然是一个字符串。 Can anyone help me get this to work?任何人都可以帮我解决这个问题吗? eval can't be used in a function/function call. eval 不能用于函数/函数调用。 I've tried exec() etc. How do I do this.我试过 exec() 等。我该怎么做。 Just need to iteratively redefine the dataframe.只需要迭代地重新定义dataframe。 Can't do that if there is an eval on each side.如果两边都有 eval,则不能这样做。 Python says each i in dataframe_list is a pyspark dataframe but when i run the code either i or dataframe_list[z] comes up as a str that has no attribute columns. Python 说 dataframe_list 中的每个 i 都是一个 pyspark dataframe 但是当我运行代码时 i 或 dataframe_list[z] 具有 noframe_list[z] 属性。 ?? ?? even though z is i+1 index for accessing the list entries....so if dataframe_list[i] is a df then dataframe_list[z] must also be one.... Any ideas?即使 z 是用于访问列表条目的 i+1 索引....所以如果 dataframe_list[i] 是 df 那么 dataframe_list[z] 也必须是一个....有什么想法吗?

You have already done eval(i) in the outer for loop so there is no need to do eval(i) in the nested for loop again.您已经在外部for循环中完成了eval(i) ,因此无需再次在嵌套for循环中执行eval(i) Also, I believe eval(dataframe[num]) should be eval(dataframe_list[num]).columns ?另外,我相信eval(dataframe[num])应该是eval(dataframe_list[num]).columns

for i in dataframe_list:
    i=eval(i)
    for num in range(1,len(dataframe_list)):
       # now use i not eval(i) like this...
       # i = i.withcolumn(column, F.lit(None))

I decided to create another list with data frame types exclusively instead of evaluating the initial list.我决定专门创建另一个具有数据框类型的列表,而不是评估初始列表。 So i added a loop:所以我添加了一个循环:

for i in dataframestr:
    i=eval(i)
    newdataframelist.append(i)

This gets around the issue of eval.这解决了 eval 的问题。

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

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