Replacing values of a column from another dataframe

前端 未结 2 1933
天命终不由人
天命终不由人 2021-01-25 08:12

Hi have a dataframe with 10000+ rows which looks like this -

df = pd.DataFrame([[\'110\', \'Demand\', 2344, 30953], 
                   [\'111\', \'Supply\', 353         


        
相关标签:
2条回答
  • 2021-01-25 08:43

    Use DataFrame.update, but first create MultiIndex by Material and Title columns in both DataFrames:

    df = df.set_index(['Material','Title'])
    extra = extra.set_index(['Material','Title'])
    
    df.update(extra)
    df = df.astype(int).reset_index()
    print (df)
      Material   Title  201950  201951
    0      110  Demand    2344   30953
    1      111  Supply      10  321312
    2      112  Supply      20    2324
    3      113  Demand   24345    4542
    4      114  Supply      30  435623
    
    0 讨论(0)
  • 2021-01-25 08:52

    You can try this:

    extra.set_index(['Material','Title']).combine_first(df.set_index(['Material','Title'])).dropna().reset_index().astype(object) 
    

    output:

      Material   Title 201950  201951
    0      110  Demand   2344   30953
    1      111  Supply     10  321312
    2      112  Supply     20    2324
    3      113  Demand  24345    4542
    4      114  Supply     30  435623
    
    0 讨论(0)
提交回复
热议问题