python-3.x - Python Pandas - 根据索引、列使用字典更新行

标签 python-3.x pandas dictionary

我有一个包含空列的数据框和一个相应的字典,我想根据索引列更新空列:

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 列 :)

最佳答案

我可以使用这样的函数,但就 Pandas 库和 DataFrame 对象的方法而言,我不确定......

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

关于python-3.x - Python Pandas - 根据索引、列使用字典更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42871723/

相关文章:

javascript - 从 Flask API 获取请求的资源上存在 "No ' Access-Control-Allow-Origin' header

python - Pandas 将 int 值转换为数据框中的 float

python Pandas : Generate a new column that calculates the subtotal of all the cells above that row in a specific column

python - 如何从数组中的值迭代字典并存储在新字典中

c++ - 无法访问指针 vector map 元素,

python - 如何将 int 的值与元组列表中的同一组组合?

python - 如何在python中排除BMP中不存在的字符?

python - 如何从 Python 装饰器内部访问装饰方法局部变量( locals() )?

Python:打印出条件for循环的结果

ios - NSDictionary 的 NSArray - 当数组中只有一个字典而不是一个对象的数组时返回 NSDictionary