2017-01-06 15 views
-3

我執行GROUPBY()和應用()的幾個數據幀具有相同的結構:熊貓GROUPBY()應用() - 從所述應用函數返回無弄亂結果

d = d.groupby('groupby_col', as_index = False).apply(some_function) 

對於一些它按預期工作,有些失敗。它失敗的方式是數據框成爲一個系列,每個元素只包含列名。它看起來像這樣:

In [18]: d.head() 
Out[18]: 
groupby_col 
134663372801   some_col_1 some_col_2 some_col_3 some_col_4... 
134663372802   some_col_1 some_col_2 some_col_3 some_col_4... 
134663372803   some_col_1 some_col_2 some_col_3 some_col_4... 
134663372804   some_col_1 some_col_2 some_col_3 some_col_4... 
134663372805   some_col_1 some_col_2 some_col_3 some_col_4... 
dtype: object 

順便說一句,該應用函數返回一個數據幀或正確的列數或無。

這可能是什麼原因以及如何進行調試?

+3

你可以張貼實際的原始數據和代碼,包括你的'some_function '有證據表明它有時會返回一個系列,而其他人則有一個DataFrame – EdChum

+0

不幸的是,我無權這麼做。 – Nucular

回答

0

問題就沒有了,如果不是從應用函數返回無我送花兒給人返回一個框架 - 更換

if some_condition: 
    return 

if some_condition: 
    return d[:0]  # return the empty frame so that the columns match 
+0

這是一個基本的python事實。你不需要任何自由來說明這一點。 – Peaceful