0

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?

Sara
  • 353
  • 1
  • 3
  • 13
  • Can you please post the desired output? It is unclear what you are trying to achieve, as there are no `date` values that can be replaced. Also, most groups only consist of just one row, what should the previous `num` be? – RJ Adriaansen Mar 11 '21 at 11:55
  • I edited the output wanted, and changed the data, the 'num' is a value that i'm looking to replace by the previous value of num within the same group, i want to replace the None values of 'num' with the previous num value on same group – Sara Mar 11 '21 at 12:15

0 Answers0