Replace NAT dates with data from another column Python Pandas

Jason Ching Yuk

I am facing difficulties replacing NAT dates with data from other column. I am using python pandas by the way.

For example

       Column1            Column2
       02-12-2006         2006
       05-12-2005         2005
       NAT                2008
       15-02-2015         2015
       NAT                2001

I have the valid dates in column1 and only the years in column2, how can i impute the NAT values using the years from column 2 based on the same row.

My desire result will be

       Column1            Column2
       02-12-2006         2006
       05-12-2005         2005
       2008               2008
       15-02-2015         2015
       2001               2001
piRSquared
df.Column1.combine_first(df.Column2)

0    2006-02-12 00:00:00
1    2005-05-12 00:00:00
2                   2008
3    2015-02-15 00:00:00
4                   2001
Name: Column1, dtype: object

Better Answer

df.Column1 = np.where(df.Column1.notnull(),
                      df.Column1.dt.strftime('%Y-%m-%d'),
                      df.Column2)

df

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace NAT dates with data from another column Python Pandas

From Dev

replace column in data frame with elements from another column - python

From Dev

Replace data from one pandas dataframe to another

From Dev

Subtracting index dates from dates in a column in pandas python

From Dev

Replace a column data with data from another column in the same table mysql

From Dev

Pandas: replace column values based on match from another column

From Dev

Efficiently replace values from a column to another column Pandas DataFrame

From Dev

Pandas find max column, subtract from another column and replace the value

From Dev

Replace contents of Excel column with realtional data from another column/sheet?

From Dev

Replace the data in a column in a file with the data in a row from another file

From Dev

Replace column values based on another dataframe python pandas - better way?

From Dev

Adding Dates (Series) column from one DataFrame to the other Pandas, Python

From Dev

Select and Replace Column Values With Data From Another Table

From Dev

Replace characters in a column, based on a translation table from another data frame

From Dev

Select and Replace Column Values With Data From Another Table

From Dev

Plot a column from a data frame as a function of another column in pandas

From Dev

Replace NAs in one column with values from another when all values are dates

From Dev

python pandas: trying to reference data from another column's previous month end

From Dev

Update a column in a data frame with the index of another column in another data frame in pandas python

From Dev

Removing duplicates from pandas data frame with condition based on another column

From Dev

Pandas: map dictionary values on an existing column based on key from another column to replace NaN

From Dev

Replace pandas column values using a key on another column and value from a dictionary

From Dev

pandas python replace column values

From Dev

Spotfire - Getting data from one table that falls between two dates in another table and adding to a calculated column

From Dev

Python - Pandas - create "first fail" column from other column data

From Dev

Adding column(s) from one dataframe to another python pandas

From Dev

Python Pandas - copy substring from one column to another

From Dev

Python/Pandas, Assign Data from one frame to Corresponding Data in Another

From Dev

Replace date with NaT in Pandas dataframe

Related Related

  1. 1

    Replace NAT dates with data from another column Python Pandas

  2. 2

    replace column in data frame with elements from another column - python

  3. 3

    Replace data from one pandas dataframe to another

  4. 4

    Subtracting index dates from dates in a column in pandas python

  5. 5

    Replace a column data with data from another column in the same table mysql

  6. 6

    Pandas: replace column values based on match from another column

  7. 7

    Efficiently replace values from a column to another column Pandas DataFrame

  8. 8

    Pandas find max column, subtract from another column and replace the value

  9. 9

    Replace contents of Excel column with realtional data from another column/sheet?

  10. 10

    Replace the data in a column in a file with the data in a row from another file

  11. 11

    Replace column values based on another dataframe python pandas - better way?

  12. 12

    Adding Dates (Series) column from one DataFrame to the other Pandas, Python

  13. 13

    Select and Replace Column Values With Data From Another Table

  14. 14

    Replace characters in a column, based on a translation table from another data frame

  15. 15

    Select and Replace Column Values With Data From Another Table

  16. 16

    Plot a column from a data frame as a function of another column in pandas

  17. 17

    Replace NAs in one column with values from another when all values are dates

  18. 18

    python pandas: trying to reference data from another column's previous month end

  19. 19

    Update a column in a data frame with the index of another column in another data frame in pandas python

  20. 20

    Removing duplicates from pandas data frame with condition based on another column

  21. 21

    Pandas: map dictionary values on an existing column based on key from another column to replace NaN

  22. 22

    Replace pandas column values using a key on another column and value from a dictionary

  23. 23

    pandas python replace column values

  24. 24

    Spotfire - Getting data from one table that falls between two dates in another table and adding to a calculated column

  25. 25

    Python - Pandas - create "first fail" column from other column data

  26. 26

    Adding column(s) from one dataframe to another python pandas

  27. 27

    Python Pandas - copy substring from one column to another

  28. 28

    Python/Pandas, Assign Data from one frame to Corresponding Data in Another

  29. 29

    Replace date with NaT in Pandas dataframe

HotTag

Archive