Issue
Assume it is a large database load from a csv, I don't know there is a "Hello" in date column.
df = {"No.": [1, 2, 3, 4],
"Date": ["17/4/2022", "18/4/2022", "Hello", "19/4/2022"]}
Output:
No. Date
1 17/4/2022
2 18/4/2022
3 Hello
4 19/4/2022
After df["Date"] = pd.to_datetime(df["Date"], infer_datetime_format=True, errors="coerce")
The "Hello" become "NaT".
Output:
No. Date
1 17/4/2022
2 18/4/2022
3 NaT
4 19/4/2022
How do I know there was a "Hello"?
How do I know what value have been changed by pd.to_datetime?
Solution
As you mentioned you want to know the changes and get only strings. You can do it this way:
import pandas as pd
df = pd.DataFrame({"No.": [1, 2, 3, 4],
"Date": ["17/4/2022", "18/4/2022", "Hello", "19/4/2022"]})
df['Changes'] = df['Date'].apply(lambda x: x if x.isalpha() else "No Change")
Output:
No. Date Changes
0 1 17/4/2022 No Change
1 2 18/4/2022 No Change
2 3 Hello Hello
3 4 19/4/2022 No Change
Answered By - Abhyuday Vaish
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.