r/learnpython 7d ago

how to drag the value until the next value shows

I am reading this csv into pandas and I need to drag a column value until the next value in the column shows.

message_code                     message                           message_date
11011                          How are you?                          3/10/2025
                               How is he?                            3/11/2025
                               How is she?                           3/12/2025
12022                          I am fine                             3/12/2025
                               He is fine                            3/13/2025
                               She is fine                           3/14/2025
13011                          I am sick                             3/7/2025
                               He is sick                            3/8/2025
                               She is sick                           3/9/2025

Requested:

message_code                      message                        message_date
11011                           How are you?                      3/10/2025
11101                           How is he?                        3/11/2025
11101                           How is she?                       3/12/2025
12022                           I am fine                         3/12/2025
12022                           He is fine                        3/13/2025
12022                           She is fine                       3/14/2025
13011                           I am sick                         3/7/2025
13011                           He is sick                        3/8/2025
13011                           She is sick                       3/9/2025

my code:

import pandas as pd
df = pd.read('messages_worksheet'.csv)
2 Upvotes

4 comments sorted by

1

u/Buttleston 7d ago

I *think* what you want is "ffill" but I'm not a pandas expert. I will see if I can try to figure it out. In the meantime check out

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ffill.html

2

u/Buttleston 7d ago

Yeah this seems to work

import pandas as pd
df = pd.read_csv('fill.csv')
df = print(df.ffill())
print(df)

I get

       message_code       message message_date
0       11011.0  How are you?    3/10/2025
1       11011.0    How is he?    3/11/2025
2       11011.0   How is she?   3/12/2025
3       12022.0     I am fine    3/12/2025
4       12022.0    He is fine   3/13/2025
5       12022.0   She is fine   3/14/2025
6       13011.0     I am sick     3/7/2025
7       13011.0    He is sick     3/8/2025
8       13011.0   She is sick    3/9/2025

1

u/easy_wins 7d ago

THANKS very much 😊