我有一个带日期的json文件,df.head()
看起来像这样:
DateTime
2015-04-21 20:00:00
2015-04-21 20:15:00
2015-04-21 20:30:00
2015-04-21 20:45:00
2015-04-21 21:00:00
我正在使用以下代码加载此json文件:
json_data = response.read().decode('utf-8', 'replace')
data = json.loads(json_data)
df = pd.json_normalize(data)
normalizedTime = pd.to_datetime(df['time'], unit='s', origin='unix').dt.floor('15T').dt
df['DateTime'] = normalizedTime.to_pydatetime()
df['Date'] = normalizedTime.date
df['Time'] = normalizedTime.time
我也有一个字符串数组,它们代表我想再次检查的某些日期:
dates = [
"2016-10-25",
"2017-01-10",
"2017-03-28",
"2017-06-13",
"2017-08-29",
"2017-10-24",
"2018-01-16",
"2018-07-21",
"2018-11-11",
"2019-03-12",
"2019-06-25",
"2019-09-24",
"2020-01-14",
"2020-07-21"
]
问题是,如果日期在dates数组中,如何将True/False
结果添加到数据框中?
df['ImportantDate'] = True/False
答案 0 :(得分:2)
将值转换为相同类型的日期字符串,并通过Series.isin
进行测试:
df['ImportantDate'] = df['Date'].astype(str).isin(dates)
如果要比较日期时间-用Series.dt.floor
删除时间(设置为00:00:00
),然后将列表转换为日期时间:
df['ImportantDate'] = normalizedTime.floor('d').isin(pd.to_datetime(dates))
比较日期:
df['ImportantDate'] = df['Date'].isin(pd.to_datetime(dates).date)
答案 1 :(得分:2)
您可以将列强制转换为str,然后检查是否isin
:
df['ImportantDate'] = df['Date'].astype(str).isin(dates)