Replacing values of a column from another dataframe

前端 未结 2 1932
天命终不由人
天命终不由人 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
    

提交回复
热议问题