0

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?

abinitio
  • 609
  • 6
  • 20
  • `df['start'] = df['start'].fillna(df['end'])`, or `df.loc[df['start'].isna(), 'start'] = df['end']` – mozway Apr 14 '23 at 09:56
  • You approach failed as `df[df["start"]=="None"]["start"]` creates a new Series, which you modify and discard on the fly. – mozway Apr 14 '23 at 09:57
  • The value I wish to replace isn't NaN, its a string "None". – abinitio Apr 14 '23 at 10:01
  • 1
    Then: `df.loc[df['start'].eq('None'), 'start'] = df['end']`. Or replace None with real NaN and use the first approach. – mozway Apr 14 '23 at 10:02

0 Answers0