2017-03-07 108 views
0

我的數據框看起來是這樣的:熊貓:有條件選擇列執行基於頭另一列的計算

(1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) (1, 9) (1, 10) (1, 11) ... 2 3 4 5 6 7 8 9 10 11 
0 0 1 0 1 1 1 1 0 1 0 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083 
1 0 0 0 0 0 0 0 0 0 0 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640 
2 1 0 0 1 0 1 1 0 0 0 ... 0.481291 0.593353 0.271028 0.498949 0.588807 0.641602 0.901779 0.424495 0.303309 0.669657 
3 1 1 0 1 0 1 1 0 0 1 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640 
4 0 0 0 1 1 1 1 1 1 1 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083 

在那裏我有列標題與像(1, 2)和列標題是一個單一的元素,就像一個元組1。我想根據具有該元組的元素的列對元組列進行計算。例如,使用元組(1, 2),我想要檢索列12,將它們相乘,然後從列(1, 2)中減去結果。

,我認爲的解決方案是創建執行從包含僅單個元件的列(例如12)第一計算(55)新的列,然後使用.where()做某種身份匹配的和all()報表。然而,這似乎相當計算效率低下,因爲我會製作其他一組數據,而不是直接在元組列上執行計算。我會怎麼做呢?

回答

2

不知道這是更快的,但這裏有一個解決方案,而無需其中()/全部()

import pandas as pd 


# create some sample data 
arr = [[1, 2, 3, 4, 5, 6, 7], 
     [7, 6, 5, 4, 3, 2, 1]] 
df = pd.DataFrame(arr, columns=[('a', 'b'), ('c','d'), ('a', 'd'), 'a', 'b', 'c', 'd']) 

# get all tuple headers 
tuple_columns = [col for col in df.columns if isinstance(col, tuple)] 

# put the results into a list of series and concat into a DataFrame 
results = pd.concat([df[col] - df[col[0]] * df[col[1]] for col in tuple_columns], axis=1) 

# rename the columns 
results.columns = tuple_columns