2017-03-18 110 views
0

我有一個空列的數據幀,並與基於索引我想更新的空列相應的字典,柱:Python的大熊貓 - 更新行基於索引的字典,列

import pandas as pd  
import numpy as np 

dataframe = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9], [4, 6, 2], [3, 4, 1]]) 
dataframe.columns = ['x', 'y', 'z'] 
additional_cols = ['a', 'b', 'c'] 

for col in additional_cols: 
    dataframe[col] = np.nan 

    x y z a b c 
0 1 2 3   
1 4 5 6   
2 7 8 9   
3 4 6 2   
4 3 4 1   

for row, column in x.iterrows(): 
    #caluclations to return dictionary y 
    y = {"a": 5, "b": 6, "c": 7} 
    df.loc[row, :].map(y) 

基本上使用列X,Y,Z進行計算後,我想更新列A,b,C表示同一行:)

回答

0

我可以用一個函數本身,而是儘可能的大熊貓庫和方法數據框對象我不知道......

def update_row_with_dict(dictionary, dataframe, index): 
    for key in dictionary.keys(): 
    dataframe.loc[index, key] = dictionary.get(key)