spcol
spcol

Reputation: 433

Set particular value for a month of data based on column in dataframe

I have a dataframe made up of daily data across a number of columns;


            A B C D
01/01/2020  12 3 2 1 
02/01/2020  8  14 5 1
03/01/2020  45 4 1 3
.
.
.
.
31/12/2021  5 1 5 3

The data is generated automatically but I would to be able to overwrite data by month or by date.

I understand something like this could reset a value but is there anyway to do it in bulk by month or between two certain dates?

df.set_value('C', 'x', 10)

Any help much appreciated!

Upvotes: 1

Views: 565

Answers (1)

jezrael
jezrael

Reputation: 862661

Create DatetimeIndex first and the set values in DataFrame.loc, also here working partialy string indexing for set values of month:

df.index = pd.to_datetime(df.index, dayfirst=True)

df.loc['2020-01-02','C'] = 100
df.loc['2020-01','B'] = 500
df.loc['2020-01-01':'2020-01-02','A'] = 0
#select multiple columns by list
df.loc['2020-01-03':'2021-12-31', ['C','D']] = 1000
print (df)
             A    B     C     D
2020-01-01   0  500     2     1
2020-01-02   0  500   100     1
2020-01-03  45  500  1000  1000
2021-12-31   5    1  1000  1000

Upvotes: 1

Related Questions