Pandas: Changing old values at list of index with with list of new values

时间:2018-01-23 19:43:34

标签: python-3.x pandas

I have a dataframe as:

dataFrame
              Price
MaturityDate        
2018-02-01     57.75
2018-03-01     41.30
2018-04-01     34.35
2018-05-01     34.05
2018-06-01     33.65
2018-07-01     38.50
2018-08-01     35.70
2018-09-01     33.50
2018-10-01     31.95
2018-11-01     32.30

I want to change the values at indexes such that new values are:

    dataFrame.ix['2018-03-01'] = 44
    dataFrame.ix['2018-02-01'] = 52.40

But my dataFrame is very large and the values at various indexes that needs updated is also very large. How do I achieve the update so that I can do something of the sort:

index2change = ['2018-02-01', '2018-03-01']
values2change = [52.40, 44]
dataFrame.loc[index2change] = values2change

1 个答案:

答案 0 :(得分:2)

您可以使用update

index2change = ['2018-02-01', '2018-03-01']
values2change = [111111, 4333334]
df.update(pd.DataFrame({'Price':values2change},index=index2change))

df

Out[584]: 
                   Price
MaturityDate            
2018-02-01     111111.00
2018-03-01    4333334.00
2018-04-01         34.35
2018-05-01         34.05
2018-06-01         33.65
2018-07-01         38.50
2018-08-01         35.70
2018-09-01         33.50
2018-10-01         31.95
2018-11-01         32.30