使用pandas.series我想获取今天的数据 数据
a= [{"content": 1, "time": 1577870427}, {"content": 4, "time": 1577870427},
{"content": 2, "time": 1577956827},
{"content": 3, "time": 1580548827}, {"content": 5, "time": 1580635227},
{"content": 6, "time": 1583054427}, {"content": 7, "time": 1582275530}]
最终数据
# today data
[{"content": 6, "time": 1583054427}, {"content": 7, "time": 1582275530}]
我的cdoe
index = pd.to_datetime([i['time'] for i in a], unit='s')
df = pd.Series(a,index)
get today data
答案 0 :(得分:1)
d = pd.to_datetime(df.str.get('time'), unit='s').dt.date
L = df[d.eq(pd.Timestamp('now').date())].tolist()
print (L)
[{'content': 7, 'time': 1582275530}]
答案 1 :(得分:1)
这是我的方法:
import datetime as dt
import pandas as pd
df = pd.DataFrame(a)
# Get a new column with the date.
df['date'] = pd.to_datetime(df['time'], unit='s').dt.date
# Subset df with today's rows.
df[df['date'] == pd.to_datetime(dt.datetime.utcnow().date())]
返回df
:
content time date
7 1582275530 2020-02-21
答案 2 :(得分:0)
from datetime import datetime
from time import time
import pandas as pd
a = [{"content": 1, "time": 1577870427},
{"content": 4, "time": 1577870427},
{"content": 2, "time": 1577956827},
{"content": 3, "time": 1580548827},
{"content": 5, "time": 1580635227},
{"content": 6, "time": 1583054427},
{"content": 7, "time": 1582275530}]
a = pd.Series(a)
for item in a:
if(datetime.fromtimestamp(item['time']).date() == datetime.fromtimestamp(time()).date()):
print(item)