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?

4
  • df['start'] = df['start'].fillna(df['end']), or df.loc[df['start'].isna(), 'start'] = df['end'] Commented Apr 14, 2023 at 9:56
  • You approach failed as df[df["start"]=="None"]["start"] creates a new Series, which you modify and discard on the fly. Commented Apr 14, 2023 at 9:57
  • The value I wish to replace isn't NaN, its a string "None". Commented Apr 14, 2023 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. Commented Apr 14, 2023 at 10:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.