如何使用字典更新DataFrame行的多个(但不是全部)值?

时间:2018-06-26 09:33:04

标签: python pandas dataframe

假设我有一个像这样的DataFrame:

import pandas as pd

df = pd.DataFrame(
         [
             ["Norway"     , 7.537, 1.5, 3.0],
             ["Denmark"    , 7.522, 1.2, 3.1],
             ["Switzerland", 7.494, 1.5, 2.8],
             ["Finland"    , 7.469, 1.6, 2.9],
             ["Netherlands", 7.377, 1.5, 3.0],
         ],
         columns = [
             "country",
             "variable_1",
             "variable_2",
             "variable_3",
         ]
    )

我该如何整洁地更新挪威的行,并使用值{"variable_2": 1.6, "variable_3": 2.9},同时确保不更改现有的variable_1值?

我在玩弄以下术语:

country_to_update = "Norway"
values_to_update  = {"variable_2": 1.6, "variable_3": 2.9}

df.query("country == @country_to_update").iloc[0] = pd.Series(values_to_update)

这会导致以下错误:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

2 个答案:

答案 0 :(得分:1)

这是一个常规解决方案

df.loc[df.country == country_to_update, values_to_update.keys()] = values_to_update.values()

Out[]: 
       country  variable_1 variable_2 variable_3
0       Norway       7.537        1.6        2.9
1      Denmark       7.522        1.2        3.1
2  Switzerland       7.494        1.5        2.8
3      Finland       7.469        1.6        2.9
4  Netherlands       7.377        1.5          3

答案 1 :(得分:1)

您可以转换为序列,然后提取索引和值:

country_to_update = 'Norway'
values_to_update = {'variable_2': 1.6, 'variable_3': 2.9}

s = pd.Series(values_to_update)

df.loc[df['country'] == country_to_update, s.index] = s.values

print(df)

       country  variable_1  variable_2  variable_3
0       Norway       7.537         1.5         3.0
1      Denmark       7.522         1.2         3.1
2  Switzerland       7.494         1.5         2.8
3      Finland       7.469         1.6         2.9
4  Netherlands       7.377         1.5         3.0