我想做什么:
# I have col names in pandas DF such as :
data 2017/01 2017/02 2017/03 ....
ABC 12 22 08 ....
EFG 07 16 12 ....
我想转换col名称:
data Jan-2017 Feb-2017 Mar-2017 ....
ABC 12 22 08 ....
EFG 07 16 12 ....
我尝试了以下内容:
pd.to_datetime(pd.Series(['2017/01']), format="%Y/%m")
其中,导致:
0 2017-01-01
dtype: datetime64[ns]
但我不确定如何得到我想要的结果。 有什么想法吗?
答案 0 :(得分:5)
你很近,需要DatetimeIndex.strftime
:
export class LoginComponent implements OnInit {
imageURL: string;
email: string;
name: string;
token: string;
constructor(private auth: AuthService, private zone: NgZone) { }
ngOnInit() {
AppGlobals.GOOGLE_CLIENT_ID = Constants.client_id;
this.getData();
setTimeout(() => {
this.googleAuthenticate();
gapi.client.load(Constants.nameProyect, 'v1', null, '//' + window.location.host + '/_ah/api');
}, 2000);
}
googleAuthenticate() {
this.auth.authenticateUser((result) => {
this.zone.run(() => {
this.getData();
});
});
}
getData() {
this.token = localStorage.getItem('token');
this.imageURL = localStorage.getItem('image');
this.name = localStorage.getItem('name');
this.email = localStorage.getItem('email');
}
logout() {
const _this = this;
this.auth.userLogout(function () {
_this.clearLocalStorage();
});
}
clearLocalStorage() {
localStorage.removeItem('token');
localStorage.removeItem('image');
localStorage.removeItem('name');
localStorage.removeItem('email');
}
}
编辑:
感谢piRSquared的想法:
df.columns = pd.to_datetime(df.columns, format="%Y/%m").strftime('%b-%Y')
print (df)
Jan-2017 Feb-2017 Mar-2017
data
ABC 12 22 8
EFG 7 16 12
答案 1 :(得分:1)
使用箭头库。箭头应该是人类的日期时间。 它干净,简单,直观。
import arrow
arrow.get('2017/03').format('MMM-YYYY')
返回Mar-2017