简体   繁体   中英

Update pandas dataframe row values from matching columns in a series/dict

I would like to update certain cell values in a pandas dataframe with the data from a pandas series or a dictionary. In the later variable, the indeces/keys match the dataframe column names but there can be fewer. For example:

import pandas as pd
import numpy as np

data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}
df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])

print df

    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6    30   -30  AAA
R4     7    40   -50  CCC

series = pd.Series(data=[1, 2, 3, 4], index=['Col1', 'Col2', 'Col3', 'Col4'])
series_inverse = pd.Series(data=[1, 2, 3, 4], index=['Col4', 'Col3', 'Col2', 'Col1'])
series_imcomplete = pd.Series(data=[2, 3], index=['Col2', 'Col3']) 

df.iloc[2] = series_imcomplete

print df

which gives:

    Col1  Col2   Col3 Col4
R1   4.0  10.0  100.0  AAA
R2   5.0  20.0   50.0  BBB
R3   NaN   2.0    3.0  NaN
R4   7.0  40.0  -50.0  CCC

This operation finds the right columns but if there are fewer entries those are replace with empty values. The desired output should be:

    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6    2.0  3.0  AAA
R4     7    40   -50  CCC

I have found solutions for columns, using replace, however I cannot make it work for rows.

I wonder if anyone could give me some advice.

try this:

In [139]: df.loc[df.index[2], series_imcomplete.index] = series_imcomplete

In [140]: df
Out[140]:
    Col1  Col2  Col3 Col4
R1     4    10   100  AAA
R2     5    20    50  BBB
R3     6     2     3  AAA
R4     7    40   -50  CCC

UPDATE: starting from Pandas 0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM