修改 Pandas Data-frame 中行值的格式
Modifying format of rows values in Pandas Data-frame
我有一个包含 70000+ 个数据点的数据集(见图)
如您所见,在 'date' 列中,一半格式(更混乱)与另一半(更清晰)不同。如何将整个格式作为数据框的后半部分?
我知道如何手动完成,但需要很长时间!
提前致谢!
编辑
df['date'] = df['date'].apply(lambda x: dt.datetime.fromtimestamp(int(str(x)) / 1000).strftime('%Y-%m-%d %H:%M:%S') if str(x).isdigit() else x)
日期的格式很奇怪
[
编辑 2
两种数据格式:
- 2012-01-0100:00:00
- 2020-07-21T22:45:00+00:00
我已经尝试了以下方法并且有效,请注意,这是假设两个关键假设:
1- 您的日期 fromat 遵循示例中两种格式中的一种且仅一种!
2-最后输出的是一个字符串!
如果是这样,这应该可以解决问题,否则,它只是一个起点,可以更改为您希望它看起来像:
import pandas as pd
import datetime
#data sample
d = {'date':['20090602123000', '20090602124500', '2020-07-22 18:45:00+00:00', '2020-07-22 19:00:00+00:00']}
#create dataframe
df = pd.DataFrame(data = d)
print(df)
date
0 20090602123000
1 20090602124500
2 2020-07-22 18:45:00+00:00
3 2020-07-22 19:00:00+00:00
#loop over records
for i, row in df.iterrows():
#get date
dateString = df.at[i,'date']
#check if it's the undesired format or the desired format
#NOTE i'm using the '+' substring to identify that, this comes to my first assumption above that you only have two formats and that should work
if '+' not in dateString:
#reformat datetime
#NOTE: this is comes to my second assumption where i'm producing it into a string format to add the '+00:00'
df['date'].loc[df.index == i] = str(datetime.datetime.strptime(dateString, '%Y%m%d%H%M%S')) + '+00:00'
else:
continue
print(df)
date
0 2009-06-02 12:30:00+00:00
1 2009-06-02 12:45:00+00:00
2 2020-07-22 18:45:00+00:00
3 2020-07-22 19:00:00+00:00
您可以格式化数据框的第一部分
import datetime as dt
df['date'] = df['date'].apply(lambda x: dt.datetime.fromtimestamp(int(str(x)) / 1000).strftime('%Y-%m-%d %H:%M:%S') if str(x).isdigit() else x)
这会检查值的所有字符是否都是数字,然后将日期格式化为第二部分
编辑
时间戳似乎以毫秒为单位,而它们应该以秒为单位 => / 1000
我有一个包含 70000+ 个数据点的数据集(见图)
如您所见,在 'date' 列中,一半格式(更混乱)与另一半(更清晰)不同。如何将整个格式作为数据框的后半部分?
我知道如何手动完成,但需要很长时间!
提前致谢!
编辑
df['date'] = df['date'].apply(lambda x: dt.datetime.fromtimestamp(int(str(x)) / 1000).strftime('%Y-%m-%d %H:%M:%S') if str(x).isdigit() else x)
日期的格式很奇怪
[
编辑 2
两种数据格式:
- 2012-01-0100:00:00
- 2020-07-21T22:45:00+00:00
我已经尝试了以下方法并且有效,请注意,这是假设两个关键假设:
1- 您的日期 fromat 遵循示例中两种格式中的一种且仅一种!
2-最后输出的是一个字符串!
如果是这样,这应该可以解决问题,否则,它只是一个起点,可以更改为您希望它看起来像:
import pandas as pd
import datetime
#data sample
d = {'date':['20090602123000', '20090602124500', '2020-07-22 18:45:00+00:00', '2020-07-22 19:00:00+00:00']}
#create dataframe
df = pd.DataFrame(data = d)
print(df)
date
0 20090602123000
1 20090602124500
2 2020-07-22 18:45:00+00:00
3 2020-07-22 19:00:00+00:00
#loop over records
for i, row in df.iterrows():
#get date
dateString = df.at[i,'date']
#check if it's the undesired format or the desired format
#NOTE i'm using the '+' substring to identify that, this comes to my first assumption above that you only have two formats and that should work
if '+' not in dateString:
#reformat datetime
#NOTE: this is comes to my second assumption where i'm producing it into a string format to add the '+00:00'
df['date'].loc[df.index == i] = str(datetime.datetime.strptime(dateString, '%Y%m%d%H%M%S')) + '+00:00'
else:
continue
print(df)
date
0 2009-06-02 12:30:00+00:00
1 2009-06-02 12:45:00+00:00
2 2020-07-22 18:45:00+00:00
3 2020-07-22 19:00:00+00:00
您可以格式化数据框的第一部分
import datetime as dt
df['date'] = df['date'].apply(lambda x: dt.datetime.fromtimestamp(int(str(x)) / 1000).strftime('%Y-%m-%d %H:%M:%S') if str(x).isdigit() else x)
这会检查值的所有字符是否都是数字,然后将日期格式化为第二部分
编辑
时间戳似乎以毫秒为单位,而它们应该以秒为单位 => / 1000