【问题标题】:Pandas concatenate/join/group rows in a dataframe based on datePandas 根据日期连接/加入/分组数据框中的行
【发布时间】:2017-04-16 21:08:55
【问题描述】:

我有一个这样的熊猫数据集:

                Date      WaterTemp   Discharge AirTemp        Precip  
0       2012-10-05 00:00       10.9      414.0    39.2           0.0   
1       2012-10-05 00:15       10.1      406.0    39.2           0.0   
2       2012-10-05 00:45       10.4      406.0    37.4           0.0   
...
63661   2016-10-12 14:30       10.5      329.0    15.8           0.0   
63662   2016-10-12 14:45       10.6      323.0    19.4           0.0   
63663   2016-10-12 15:15       10.8      329.0      23           0.0   

我想扩展每一行,以便得到一个如下所示的数据集:

              Date    WaterTemp 00:00    WaterTemp 00:15 .... Discharge 00:00 ...
0       2012-10-05                10.9              10.1                414.0

每个日期最多有 72 个读数,所以除了日期和索引列之外,我应该有 288 列,并且我最多应该有 1460 行(4 年 * 一年中的 365 天 - 可能缺少一些日期)。最终,我将在分类任务中使用 288 列数据集(稍后我将添加标签),因此我需要将此数据帧转换为二维数组(无日期时间)以输入分类器,所以我可以t 只需按日期分组,然后访问该组。我确实尝试过根据日期进行分组,但我不确定如何将每个组更改为一行。我也考虑加入。看起来加入可以满足我的需求(例如基于(日、月、年)的加入),但我不确定如何将事物拆分为不同的 pandas 数据框以便加入工作。有什么方法可以做到这一点?

PS。我已经知道如何将我的日期列中的日期时间更改为没有时间的日期。

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    我想通了。我按一天中的阅读时间对阅读内容进行分组。每个组本身就是一个数据框,所以我只需要根据日期连接数据框。我的整个函数的代码如下。

    import pandas
    
    def readInData(filename):
        #read in files and remove missing values
        ds = pandas.read_csv(filename) 
        ds = ds[ds.AirTemp != 'M']
        #set index to date
        ds['Date'] = pandas.to_datetime(ds.Date, yearfirst=True, errors='coerce')
        ds.Date = pandas.DatetimeIndex(ds.Date)
        ds.index = ds.Date
        #group by time (so group readings by time of day of reading, i.e. all readings at midnight)
        dg = ds.groupby(ds.index.time)
    
        #initialize the final dataframe
        df = pandas.DataFrame()
        for name, group in dg: #for each group
            #each group is a dateframe
            try:
                #set unique column names except for date
                group.columns = ['Date', 'WaterTemp'+str(name), 'Discharge'+str(name), 'AirTemp'+str(name), 'Precip'+str(name)]
                #ensure date is the index            
                group.index = group.Date
                #remove time from index
                group.index = group.index.normalize()
                #join based on date
                df = pandas.concat([df, group], axis=1)
            except: #if the try catch block isn't here, throws errors! (three for my dataset?)
                pass
        #remove duplicate date columns
        df = df.loc[:,~df.columns.duplicated()]
        #since date is index, drop the first date column
        df = df.drop('Date', 1)
        #return the dataset
        return df
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2016-06-24
      • 2022-11-12
      • 1970-01-01
      • 2016-10-08
      • 2014-12-07
      相关资源
      最近更新 更多