熊猫-每天获取一个数据框

时间:2018-12-07 09:31:31

标签: python pandas datetime

我有一个DataFrame,索引中有日期。我每天都做一个DataFrame的子集。有什么方法可以编写函数或循环来自动生成这些步骤?

import json
import requests
import pandas as pd
from pandas.io.json import json_normalize
import datetime as dt

#Get the channel feeds from Thinkspeak
response = requests.get("https://api.thingspeak.com/channels/518038/feeds.json?api_key=XXXXXX&results=500")

#Convert Json object to Python object
response_data = response.json()
channel_head = response_data["channel"]
channel_bottom = response_data["feeds"]

#Create DataFrame with Pandas
df = pd.DataFrame(channel_bottom)

#rename Parameters
df = df.rename(columns={"field1":"PM 2.5","field2":"PM 10"})

#Drop all entrys with at least on nan
df = df.dropna(how="any")

#Convert time to datetime object
df["created_at"] = df["created_at"].apply(lambda x:dt.datetime.strptime(x,"%Y-%m-%dT%H:%M:%SZ"))

#Set dates as Index
df = df.set_index(keys="created_at")

#Make a DataFrame for every day
df_2018_12_07 = df.loc['2018-12-07']
df_2018_12_06 = df.loc['2018-12-06']
df_2018_12_05 = df.loc['2018-12-05']
df_2018_12_04 = df.loc['2018-12-04']
df_2018_12_03 = df.loc['2018-12-03']
df_2018_12_02 = df.loc['2018-12-02']

3 个答案:

答案 0 :(得分:1)

您可以使用:

from datetime import date
today = str(date.today())
df = df.loc[today]

并使用诸如crontab之类的任何调度程序来调度脚本。

答案 1 :(得分:1)

您可以创建dictionary of DataFrames-然后为keyDataFrame个进行选择:

dfs = dict(tuple(df.groupby(df.index.strftime('%Y-%m-%d'))))

print (dfs['2018-12-07'])

答案 2 :(得分:1)

假设您在下周的第一天执行此操作(因此,将星期一导出到下周一的星期日,您可以执行以下操作:

from datetime import date, timedelta
day = date.today() - timedelta(days=7)   # so, if today is monday, we start monday before
df = df.loc[today]
while day < today:
    df1 = df.loc[str(day)]
    df1.to_csv('mypath'+str(day)+'.csv')  #so that export files have different names 
    day = day+ timedelta(days=1)