我有三个很长的Pandas数据帧列表。例如:
list_a = [tablea1, tablea2, tablea3, tablea4]
list_b = [tableb1, tableb2, tableb3, tableb4]
list_c = [tablec1, tablec2, tablec3, tablec4]
我想这样做:
^{pr2}$太天真了,我写了这样的代码:
for i in range(len(list_a)):
list_a[i] = pd.concat([list_a[i], list_b[i], list_c[i]], axis=1)
此代码无法工作,b/c列表_a[0]最初是对表A1的引用,然后在循环内,列表_a[0]将被重新赋值指向
pd.concat([tablea1, tableb1, tablec1], axis=1),
这是一个新对象。最后,表A1没有修改。(列表_a确实包含所需的结果。但我确实想修改表1。)我花了好几个小时在这上面,却找不到解决方案。有什么帮助吗?谢谢。在
@qqzj你会遇到的问题是python没有这个特性。作为@Boudmentions,对tablea1、tableb1、tablec1等的引用在连接后丢失。在
我将举例说明一个快速而肮脏的变通方法(这是非常低效的,但可以完成工作)。在
没有你的数据,我基本上是在创建随机的数据帧。在
应用你的代码来迭代这个列表
^{pr2}$在这里运行比较之后,您会看到突出显示的问题,即虽然}连接,但它没有被分配回
list_a[i]
与tablea1
、tableb1
、和{tablea1
。在正如我在评论中提到的,答案是用列表[0]分配
tablea1
您可以对
tablea2
tablea3
等重复此操作进行比较后,现在可以看到tablea1与list[0]中的值相匹配
同样,这不是理想的解决方案,但你所寻找的似乎不是“Python式”的方式。在
感谢zhqiat的示例代码。让我再详谈一下。在这里,这个问题可以用exec语句来解决。在
我用这种方法已经有一段时间了。但是在代码变得更复杂之后。执行官开始抱怨了
^{pr2}$以下是问题代码:
此代码将生成错误消息。有趣的是,在我的实际函数中根本没有其他“def”语句。所以我没有嵌套函数。我很困惑为什么我收到这样的错误信息。我的问题是,有没有一种方法可以让Python告诉我哪个变量是罪魁祸首,也就是导致问题的自由变量?或者,哪一个子函数是导致代码失败的原因。理想情况下,对于这个例子,我希望我可以强迫python告诉我dummy_函数是原因。在
相关问题 更多 >
编程相关推荐