问题
I have a sample data as below:
date Deadline
2018-08-01
2018-08-11
2018-09-18
2018-12-08
2018-12-18
I want to fill in the deadline column with the conditions described in the code as "1 DL", "2 DL", "3 DL" and so on.
Creating a new column based on the date column in python.
It giving an error:
('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
I have tried as below:
df['date'] = pd.to_datetime(df['date'], format = "%y-%m-%d").dt.date
def dead_line(df5):
if((df5['date'] >= datetime.date(2018, 8, 1)) & (df['date'] <= datetime.date(2018, 9, 14))):
return "1 DL"
elif ((df5['date'] >= datetime.date(2018, 9, 15)) & (df5['date'] <= datetime.date(2018, 10, 17))):
return "2 DL"
elif ((df5['date'] >= datetime.date(2018, 10, 18)) & (df5['date'] <= datetime.date(2018, 12, 5))):
return "3 DL"
elif ((df5['date'] >= datetime.date(2018, 12, 6)) & (df5['date'] <= datetime.date(2019, 2, 1))):
return "4 DL & EDL 2"
df['Deadline'] = df.apply(dead_line, axis = 1)
Expected Output:
date Deadline
2018-08-01 1 DL
2018-09-16 2 DL
2018-12-07 3 DL
and so on.
回答1:
A different solution to the one above. Do not convert your datetime to a datetime object for comparison, instead leave it as datetime64, then apply your filter function to other datetime64 ranges:
df['date'] = pd.to_datetime(df['date'], format = "%Y-%m-%d") # leaves as datetime64[ns]
print(df['date'].dtype) #datetime64[ns]
def dead_line(x):
if (x >= pd.to_datetime('2018-08-01')) & (x <= pd.to_datetime('2018-09-14')):
return "1 DL"
elif (x >= pd.to_datetime('2018-09-15')) & (x <=pd.to_datetime('2018-10-17')):
return "2 DL"
elif (x >= pd.to_datetime('2018-10-18')) & (x <= pd.to_datetime('2018-12-05')):
return "3 DL"
elif (x >=pd.to_datetime('2018-12-06')) & (x <= pd.to_datetime('2019-02-01')):
return "4 DL & EDL 2"
df['Deadline'] = df['date'].apply(dead_line) # apply your function to column, not whole df
print(df)
output:
date Deadline
0 2018-08-01 1 DL
1 2018-08-11 1 DL
2 2018-09-18 2 DL
3 2018-12-08 4 DL & EDL 2
4 2018-12-18 4 DL & EDL 2
回答2:
Use pd.cut to bin categoricals
The core problem is you are attempting column-wise operations with apply along axis=1
. Yet apply
here requires row-wise operations.
That said, with Pandas you are better off using vectorised column-wise operations. So don't use apply
, use vectorised pd.cut
instead. Notice also there's no need to resort to Python datetime
.
# convert series to datetime
df['date'] = pd.to_datetime(df['date'])
# remember to include arbitrary lower and upper boundaries
L = ['01-01-2000', '08-01-2018', '09-14-2018', '10-17-2018',
'12-05-2018', '02-01-2019', '01-01-2100']
# convert boundaries to datetime
dates = pd.to_datetime(L).values
# define labels for boundary ranges
labels = ['Error Lower', '1 DL', '2 DL', '3 DL', '4 DL & EDL 2', 'Error Upper']
# apply categorical binning
df['Deadline'] = pd.cut(df['date'], bins=dates, labels=labels, right=False)
print(df)
# date Deadline
# 0 2018-08-01 1 DL
# 1 2018-08-11 1 DL
# 2 2018-09-18 2 DL
# 3 2018-12-08 4 DL & EDL 2
# 4 2018-12-18 4 DL & EDL 2
来源:https://stackoverflow.com/questions/54099465/filtering-and-creating-a-column-based-on-the-date-column