felthefel
felthefel

Reputation: 33

Overwriting data of similar length in dataframe

Lets say I have a df like this: enter image description here

I want to take the data in w at the day before and put it in w the day after. So just want to overwrite the data on w 1929-02-02 with the data on w from 1929-02-01. All the other columns should be as is. They are similar length!

I have tried something like this, but it just puts in nan. enter image description here

enter image description here it is pretty much like saying. B on row 3 and 4 is equal to B on row 1 and 2

This code can be used for testing. Just want all values of w at first date, to overwrite all values of w at second date. The counter is used because this is just a simplification of the problem in a loop.

  a = {'PERMNO' : pd.Series(["10006","10007", "10008", "10006","10007", "10008"], index=['1929-02-01', '1929-02-01', '1929-02-01','1929-02-02', '1929-02-02', '1929-02-02']), 
   'w' : pd.Series([0.25, 0.5, 0.25, 0.33,0.33,0.33], index=['1929-02-01', '1929-02-01', '1929-02-01','1929-02-02', '1929-02-02', '1929-02-02']),'counter' : pd.Series([1, 2, 3, 4,5,6], index=['1929-02-01', '1929-02-01', '1929-02-01','1929-02-02', '1929-02-02', '1929-02-02'])}
df= pd.DataFrame(a)
df.reset_index(inplace=True)

Upvotes: 0

Views: 53

Answers (1)

akocz
akocz

Reputation: 138

You could use .loc for indexing the dataframe as the error message suggests.

data.loc[(data["counter"] < 901) & (data["counter"] >= 451), "w"] = change0["w"]

Upvotes: 1

Related Questions