날짜를 찾을 수있는 다른 열을 기반으로 월-년을 표시하는 새 열을 만들고 싶습니다.
다음 코드를 만들었습니다.
BusinessObjects_na_['Customer Handover Date'] = pd.to_datetime(BusinessObjects_na_['Customer Handover Date'])
BusinessObjects_na_['Month'] = BusinessObjects_na_['Customer Handover Date'].apply(lambda x: str(x).strftime('%B-%Y'))
그러나 Python은 다음 오류를 제공합니다.
AttributeError: 'str' object has no attribute 'strftime'
누군가 이것으로 나를 도울 수 있습니까?
최신 버전의 pandas를 실행하는 경우 다음을 사용할 수 있습니다 dt.strftime
.
BusinessObjects_na_['Month'] = BusinessObjects_na_['Customer Handover Date'].dt.strftime('%B-%Y')
코드는`str :
BusinessObjects_na_['Month'] = BusinessObjects_na_['Customer Handover Date'].apply(lambda x: x.strftime('%B-%Y'))
예:
In [276]:
df = pd.DataFrame({'date':pd.date_range(dt.datetime(2016,1,1), periods=10)})
df
Out[276]:
date
0 2016-01-01
1 2016-01-02
2 2016-01-03
3 2016-01-04
4 2016-01-05
5 2016-01-06
6 2016-01-07
7 2016-01-08
8 2016-01-09
9 2016-01-10
In [279]:
df['date'].dt.strftime('%b-%Y')
Out[279]:
0 Jan-2016
1 Jan-2016
2 Jan-2016
3 Jan-2016
4 Jan-2016
5 Jan-2016
6 Jan-2016
7 Jan-2016
8 Jan-2016
9 Jan-2016
Name: date, dtype: object
과
In [281]:
df['date'].apply(lambda x: x.strftime('%b-%Y'))
Out[281]:
0 Jan-2016
1 Jan-2016
2 Jan-2016
3 Jan-2016
4 Jan-2016
5 Jan-2016
6 Jan-2016
7 Jan-2016
8 Jan-2016
9 Jan-2016
Name: date, dtype: object
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다