I have a sample of a data frame which looks like this:
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| | Date | Professional | Description |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 0 | 2019-12-19 00:00:00 | Katie Cool | Travel to Space ... |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 1 | 2019-12-20 00:00:00 | Jenn Blossoms | Review stuff; prepare cancellations of ... |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 2 | 2019-12-27 00:00:00 | Jenn Blossoms | Review lots of stuff/o... |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 3 | 2019-12-27 00:00:00 | Jenn Blossoms | Draft email to world leader... |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 4 | 2019-12-30 00:00:00 | Jenn Blossoms | Review this thing. |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
| 5 | 12-30-2019 Jenn Blossoms Telephone Call to A. Bell return her multiple | NaN | NaN |
| | voicemails. | | |
+---+--------------------------------------------------------------------------------------+---------------+--------------------------------------------+
Much of the row's data is in the date cell.
I would like for the sample to look like this:
+---+---------------------+---------------+-------------------------------------------------------------+
| | Date | Professional | Description |
+---+---------------------+---------------+-------------------------------------------------------------+
| 0 | 2019-12-19 00:00:00 | Katie Cool | Travel to Space ... |
+---+---------------------+---------------+-------------------------------------------------------------+
| 1 | 2019-12-20 00:00:00 | Jenn Blossoms | Review stuff; prepare cancellations of ... |
+---+---------------------+---------------+-------------------------------------------------------------+
| 2 | 2019-12-27 00:00:00 | Jenn Blossoms | Review lots of stuff/o... |
+---+---------------------+---------------+-------------------------------------------------------------+
| 3 | 2019-12-27 00:00:00 | Jenn Blossoms | Draft email to world leader... |
+---+---------------------+---------------+-------------------------------------------------------------+
| 4 | 2019-12-30 00:00:00 | Jenn Blossoms | Review this thing. |
+---+---------------------+---------------+-------------------------------------------------------------+
| 5 | 12-30-2019 | Jenn Blossoms | Telephone Call to A. Bell return her multiple |
| | | | voicemails. |
+---+---------------------+---------------+-------------------------------------------------------------+
I have tried this code:
date = dftopdata['Date'].str.extract('(d{2}-d{2}-d{4})(sw+sw+)s(w+.*)')[0]
name = dftopdata['Date'].str.extract('(d{2}-d{2}-d{4})(sw+sw+)s(w+.*)')[1]
description = dftopdata['Date'].str.extract('(d{2}-d{2}-d{4})(sw+sw+)s(w+.*)')[2]
dftopdata.loc[pd.to_datetime(dftopdata['Date'],errors='coerce').isnull(),'Professional'] = name
dftopdata.loc[pd.to_datetime(dftopdata['Date'],errors='coerce').isnull(),'Description'] = description
dftopdata.loc[pd.to_datetime(dftopdata['Date'],errors='coerce').isnull(),'Date'] = date
But when I run the above code, the data frame sample looks like this:
+---+------------+---------------+--------------------------------------------+
| | Date | Professional | Description |
+---+------------+---------------+--------------------------------------------+
| 0 | 12/19/2019 | Katie Cool | Travel to space ... |
+---+------------+---------------+--------------------------------------------+
| 1 | 12/20/2019 | Jenn Blossoms | Review stuff; prepare cancellations of ... |
+---+------------+---------------+--------------------------------------------+
| 2 | 12/27/2019 | Jenn Blossoms | Review lots of stuff/o… |
+---+------------+---------------+--------------------------------------------+
| 3 | 12/27/2019 | Jenn Blossoms | Draft email to world leader... |
+---+------------+---------------+--------------------------------------------+
| 4 | 12/30/2019 | Jenn Blossoms | Review this thing. |
+---+------------+---------------+--------------------------------------------+
| 5 | NaN | NaN | NaN |
+---+------------+---------------+--------------------------------------------+
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…