I have a pandas dataframe that looks something like this:
myid user start end
a tom 2023-01-01T23:41:32 2023-01-02T23:41:32
b dick None 2023-01-05T20:41:32
c harry 2023-01-01T23:41:32 2023-01-03T21:41:32
d sally None 2023-01-05T03:41:32
I am trying to replace the values of df["start"] which are equal to "None" with the respective value of df["end"] for that row entry:
myid user start end
a tom 2023-01-01T23:41:32 2023-01-02T23:41:32
b dick 2023-01-05T20:41:32 2023-01-05T20:41:32
c harry 2023-01-01T23:41:32 2023-01-03T21:41:32
d sally 2023-01-05T03:41:32 2023-01-05T03:41:32
This doesn't seem to work:
df[df["start"]=="None"]["start"]=df[df["start"]=="None"]["end"]
The entries with "start"== "None" remain unchanged.
Is there a simple way to achieve this?
df['start'] = df['start'].fillna(df['end']), ordf.loc[df['start'].isna(), 'start'] = df['end']df[df["start"]=="None"]["start"]creates a new Series, which you modify and discard on the fly.df.loc[df['start'].eq('None'), 'start'] = df['end']. Or replace None with real NaN and use the first approach.