问题
I'd like to convert a dataframe column which has a date string. But in some cases, the date string might be empty due to certain conditions. So I just want all the other rows in that column to be converted to datetime format except the rows in that particular column which might be blank. Is it possible?
What I've tried so far:
Option1:
df['etime'] = pd.to_datetime(df['etime'],errors='ignore').dt.strftime('%Y-%m-%d %H:%M')
Option 2:
for ind in df.index:
if (df['etime'].str.len()[ind] == 0) :
df.loc[ind, 'etime'] = "----"
else:
df.loc[ind, 'etime'] = <need to convert this row to datetime>
Kindly provide your suggestions.
Dataframe sample:
data = pd.DataFrame({
'day' : [15, 17, 20, 14, 25],
'etime': ["20200811235205", "", "20200811215205", "20200811225205", "20200811235203"]
})
回答1:
Two steps,
first lets create a series with your datetimes and coerce the bad values into NaTs
s = pd.to_datetime(data['etime'],errors='coerce',format='%Y%m%d%H%M%S')
second, lets find any values that aren't NaT
and replace them with your target formatting.
data.loc[~s.isna(),'etime'] = s.dt.strftime('%Y-%m-%d %H:%M')
day etime
0 15 2020-08-11 23:52
1 17
2 20 2020-08-11 21:52
3 14 20200811265205
4 25 2020-08-11 23:52
- assuming 26 is a typo in your hour column at index 3.
回答2:
You can try something like:
df["etime"] = df["etime"].apply(lambda x: pd.to_datetime(x,errors='ignore').strftime('%Y-%m-%d %H:%M') if len(x) !=0 else "----")
来源:https://stackoverflow.com/questions/63393340/convert-dataframe-column-to-datetime-only-if-length-of-string-is-not-zero