我有以下日期字段为object
的pandas数据框
ID Date Volume
0 13-02-2018 00:06 85
1 13-02-2018 00:10 70
2 13-02-2018 00:11 100
3 2018-02-13 06:30 123
4 02-13-2018 07:56 100
我想将其转换为以下一种格式
ID Date Volume
0 2018-02-13 00:06 85
1 2018-02-13 00:10 70
2 2018-02-13 00:11 100
3 2018-02-13 06:30 123
4 2018-02-13 07:56 100
我正在尝试通过以下命令来实现这一目标
df['Date'] = df.date.apply(lambda x: pd.to_datetime(x).strftime('%Y-%m-%d %H:%M')[0])
但是会引发错误。我该怎么做在熊猫里?
答案 0 :(得分:3)
答案 1 :(得分:0)
示例:
df['Date'] = ['13-02-2018 00:06', '13-02-2018 00:06']
如果您也可以使用小时格式,就只需要这个:
df['Date'] = pd.to_datetime(df['Date'])
或者您尝试的方式:
df['Date'] = df['Date'].apply(lambda x: pd.to_datetime(x))
均提供以下信息:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 1 columns):
Date 2 non-null datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 96.0 bytes
现在是日期时间格式!
答案 2 :(得分:-1)
导入时间
...
String s = "";
for (DataSnapshot child: dataSnapshot.getChildren()){
//Object object = child.getKey();
s = s.concat(child.getKey());
//s = s.concat(" "); if you want to have a white-space in between keys.
//s = s.concat("\n") if you want to have every key on a new line.
}
labelGetKey.setText(s);
...
检查这是否可行