如何批量处理Python-Pandas数据帧?

2024-09-30 19:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个很长的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。)我花了好几个小时在这上面,却找不到解决方案。有什么帮助吗?谢谢。在


Tags: 数据代码pandas列表a1listpdaxis
2条回答

@qqzj你会遇到的问题是python没有这个特性。作为@Boudmentions,对tablea1、tableb1、tablec1等的引用在连接后丢失。在

我将举例说明一个快速而肮脏的变通方法(这是非常低效的,但可以完成工作)。在

没有你的数据,我基本上是在创建随机的数据帧。在

tablea1 = pd.DataFrame(np.random.randn(10, 4))
tableb1 = pd.DataFrame(np.random.randn(10, 4))
tablec1 = pd.DataFrame(np.random.randn(10, 4))

tablea2 = pd.DataFrame(np.random.randn(10, 4))
tableb2 = pd.DataFrame(np.random.randn(10, 4))
tablec2 = pd.DataFrame(np.random.randn(10, 4))

应用你的代码来迭代这个列表

^{pr2}$

在这里运行比较之后,您会看到突出显示的问题,即虽然list_a[i]tablea1tableb1、和{}连接,但它没有被分配回tablea1。在

正如我在评论中提到的,答案是用列表[0]分配tablea1

tablea1=list_a[0]

您可以对tablea2tablea3等重复此操作

进行比较后,现在可以看到tablea1与list[0]中的值相匹配

tablea1==list_a[0]

      0     1     2     3     0     1     2     3     0     1     2     3
0  True  True  True  True  True  True  True  True  True  True  True  True
1  True  True  True  True  True  True  True  True  True  True  True  True
2  True  True  True  True  True  True  True  True  True  True  True  True
3  True  True  True  True  True  True  True  True  True  True  True  True
4  True  True  True  True  True  True  True  True  True  True  True  True
5  True  True  True  True  True  True  True  True  True  True  True  True
6  True  True  True  True  True  True  True  True  True  True  True  True
7  True  True  True  True  True  True  True  True  True  True  True  True
8  True  True  True  True  True  True  True  True  True  True  True  True
9  True  True  True  True  True  True  True  True  True  True  True  True

同样,这不是理想的解决方案,但你所寻找的似乎不是“Python式”的方式。在

感谢zhqiat的示例代码。让我再详谈一下。在这里,这个问题可以用exec语句来解决。在

import pandas as pd
import numpy as np

tablea1 = pd.DataFrame(np.random.randn(10, 4))
tableb1 = pd.DataFrame(np.random.randn(10, 4))
tablec1 = pd.DataFrame(np.random.randn(10, 4))

tablea2 = pd.DataFrame(np.random.randn(10, 4))
tableb2 = pd.DataFrame(np.random.randn(10, 4))
tablec2 = pd.DataFrame(np.random.randn(10, 4))

list_a = [tablea1, tablea2]
list_b = [tableb1, tableb2]
list_c = [tablec1, tablec2]

for i in range(1, len(list_a)+1):
    exec 'tablea' + str(i) + ' = pd.concat([tablea' + str(i) + ', ' + 'tableb' + str(i) + ', ' +  'tablec' + str(i) + '], axis=1)'

print tablea1

我用这种方法已经有一段时间了。但是在代码变得更复杂之后。执行官开始抱怨了

^{pr2}$

以下是问题代码:

def overall_function():

    def dummy_function():
        return True

    tablea1 = pd.DataFrame(np.random.randn(10, 4))
    tableb1 = pd.DataFrame(np.random.randn(10, 4))
    tablec1 = pd.DataFrame(np.random.randn(10, 4))

    tablea2 = pd.DataFrame(np.random.randn(10, 4))
    tableb2 = pd.DataFrame(np.random.randn(10, 4))
    tablec2 = pd.DataFrame(np.random.randn(10, 4))

    list_a = ['tablea1', 'tablea2']
    list_b = ['tableb1', 'tableb2']
    list_c = ['tablec1', 'tablec2']

    for i, j, k in zip(list_a, list_b, list_c):
        exec(i + ' = pd.concat([' + i + ',' + j + ',' + k + '], axis=1)')


    print tablea1

overall_function()

此代码将生成错误消息。有趣的是,在我的实际函数中根本没有其他“def”语句。所以我没有嵌套函数。我很困惑为什么我收到这样的错误信息。我的问题是,有没有一种方法可以让Python告诉我哪个变量是罪魁祸首,也就是导致问题的自由变量?或者,哪一个子函数是导致代码失败的原因。理想情况下,对于这个例子,我希望我可以强迫python告诉我dummy_函数是原因。在

相关问题 更多 >