簡體   English   中英

如何批量處理Python Pandas數據幀?

[英]How to process Python Pandas data frames in batches?

我有三個非常長的Pandas數據框列表。 例如:

list_a = [tablea1, tablea2, tablea3, tablea4]

list_b = [tableb1, tableb2, tableb3, tableb4]

list_c = [tablec1, tablec2, tablec3, tablec4]

我想做這樣的事情:

tablea1 = pd.concat([tablea1, tableb1, tablec1], axis=1)

因此,我天真地寫了這樣的代碼:

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 list_a [0]最初是對tablea1的引用,然后在循環內部,將list_a [0]重新分配為指向

pd.concat([tablea1, tableb1, tablec1], axis=1), 

這是一個新對象。 最后,tablea1未被修改。 (list_a確實包含了所需的結果。但是我確實想修改tablea1。)我已經花了數小時在此上並且找不到解決方案。 有什么幫助嗎? 謝謝。

@qqzj您將遇到的問題是python並不完全具有此功能。 正如@Boud所提到的 ,連接后對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))

應用您的代碼以遍歷此列表

list_a = [tablea1, tablea2]
list_b = [tableb1, tableb2]
list_c = [tablec1, tablec2]
for i in range(len(list_a)):
    list_a[i] = pd.concat([list_a[i], list_b[i], list_c[i]], axis=1)

在此處進行比較后,您會看到突出顯示的問題,即list_a[i]已與tablea1tableb1tablec1連接在一起, tableb1尚未將其分配回tablea1

正如我在評論中提到的,答案是為tablea1分配列表[0]

tablea1=list_a[0]

您將對tablea2 tablea3等重復此tablea3

進行比較后,您現在可以看到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

同樣,這不是理想的解決方案,但是您正在尋找的似乎不是“ pythonic”方式。

感謝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

我使用這種方法已有一段時間了。 但是在代碼變得更加復雜之后。 高管開始抱怨

'SyntaxError: unqualified exec is not allowed in function 'function name' it contains a nested function with free variables'. 

這是有問題的代碼:

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_function是原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM