I have a df
df:
date shares symbol date2
0 20120614 1100 AAT.N NaN
1 20120615 1100 AAT.N NaN
2 20120616 1100 AAT.N NaN
3 20120617 1100 AAT.N NaN
4 20030405 800 ABT.N NaN
5 20030406 800 ABT.N NaN
6 20030407 800 ABT.N NaN
...
#This is what I want:
df:
date shares symbol date2
0 20120614 1100 AAT.N 20120615
1 20120615 1100 AAT.N 20120616
2 20120616 1100 AAT.N 20120617
3 20120617 1100 AAT.N NaN
4 20030405 800 ABT.N 20030406
5 20030406 800 ABT.N 20030407
6 20030407 800 ABT.N NaN
...
I want to replace df.ix[0]['date2'] with df.ix[1]['date2'] for each symbol -- the symbol changes through the dataframe so I can't just apply this through the whole dataframe.
I was going to loop through and if the symbol for i and i+1 matched:
df.ix[i]['symbol'] == df.ix[i+1]['symbol']
I was going to replace the NaN with the date.
I tried:
df.ix[i]['date2'] = df.ix[i+1]['date'] ##This failed.
I then tried:
a = df.ix[i+1]['date']
df.replace({'date2': i}, a)
###This failed as well
Any recommendations here on
1) Best process to accomplish this?
2) Basic question: How to replace an NaN (or even another number) in a pandas DF?
Thank you.