I have this data structure :
data = [(1, '5G', '620'), ( '', '5G', '620'), ( 3, '3G', '620'),('', '6G', '620'), (267, '3G', '620')]
df = pd.DataFrame(data, columns=['num', 'ticker', 'value'])
print(fd)
date ticker value
0 1 5GA 620
1 None 5GA 620
2 3 3GA 620
3 None 6HD 620
4 267 6HD 620
By grouping by on the ticker
and value
I want to replace the date
with the previous num
within the group when the num
is none:
for index, row in df.groupby(['ticker','value']).iterrows():
df.loc[pd.isnull(df['num']), 'date'] = df.loc[df['num']]
print(df)
the output should be as follows :
date ticker value
0 1 5GA 620
1 1 5GA 620
2 3 3GA 620
3 267 6HD 620
4 267 6HD 620
I'm stuck, how can I do that?