用 Series/dictionary 中定义的值替换部分 df 列
Replace part of df column with values defined in Series/dictionary
我在 DataFrame
中有一列经常有重复索引。有些索引有例外,需要根据我所做的另一个 Series
进行更改,而其余索引则没有问题。 Series
索引是唯一的。
这里有几个变量来说明
df = pd.DataFrame(data={'hi':[1, 2, 3, 4, 5, 6, 7]}, index=[1, 1, 1, 2, 2, 3, 4])
Out[52]:
hi
1 1
1 2
1 3
2 4
2 5
3 6
4 7
exceptions = pd.Series(data=[90, 95], index=[2, 4])
Out[36]:
2 90
4 95
我想将 df 设置为...
hi
1 1
1 2
1 3
2 90
2 90
3 6
4 95
执行此操作的简洁方法是什么?我对 Pandas 有点陌生,我的想法只是循环,但我认为这不是解决这个问题的正确方法
假设 exceptions
中的索引保证是 df
索引的子集,我们可以使用 loc
and the Series.index
来分配值:
df.loc[exceptions.index, 'hi'] = exceptions
如果我们在 exceptions
中有额外的值,但在 df
:
中没有或不应该对齐,我们可以使用 index.intersection
exceptions = pd.Series(data=[90, 95, 100], index=[2, 4, 5])
df.loc[exceptions.index.intersection(df.index, sort=False), 'hi'] = exceptions
df
:
hi
1 1
1 2
1 3
2 90
2 90
3 6
4 95
我在 DataFrame
中有一列经常有重复索引。有些索引有例外,需要根据我所做的另一个 Series
进行更改,而其余索引则没有问题。 Series
索引是唯一的。
这里有几个变量来说明
df = pd.DataFrame(data={'hi':[1, 2, 3, 4, 5, 6, 7]}, index=[1, 1, 1, 2, 2, 3, 4])
Out[52]:
hi
1 1
1 2
1 3
2 4
2 5
3 6
4 7
exceptions = pd.Series(data=[90, 95], index=[2, 4])
Out[36]:
2 90
4 95
我想将 df 设置为...
hi
1 1
1 2
1 3
2 90
2 90
3 6
4 95
执行此操作的简洁方法是什么?我对 Pandas 有点陌生,我的想法只是循环,但我认为这不是解决这个问题的正确方法
假设 exceptions
中的索引保证是 df
索引的子集,我们可以使用 loc
and the Series.index
来分配值:
df.loc[exceptions.index, 'hi'] = exceptions
如果我们在 exceptions
中有额外的值,但在 df
:
index.intersection
exceptions = pd.Series(data=[90, 95, 100], index=[2, 4, 5])
df.loc[exceptions.index.intersection(df.index, sort=False), 'hi'] = exceptions
df
:
hi
1 1
1 2
1 3
2 90
2 90
3 6
4 95