Pandas 同一数据帧中具有相同日期但不同时间的多个文件

Pandas 同一数据帧中具有相同日期但不同时间的多个文件,pandas,python-2.7,Pandas,Python 2.7,我必须读取不同目录中的多个文件,然后将它们放在同一数据帧中。我正在使用csv文件读取日期 文件名被组织为hgyymddhhhh yy=年 毫米=月 dd=天 hh=小时 问题是:对于同一天,我可以有1、2或3个不同的小时: hg1308250000.nc hg1308250100.nc hg1308250200.nc df=pd.read_csv('/home/Desktop/dates.csv') df['Date'] = pd.to_datetime(df['Date']

我必须读取不同目录中的多个文件,然后将它们放在同一数据帧中。我正在使用csv文件读取日期

文件名被组织为hgyymddhhhh

yy=年

毫米=月

dd=天

hh=小时

问题是:对于同一天,我可以有1、2或3个不同的小时:

hg1308250000.nc

hg1308250100.nc

hg1308250200.nc

 df=pd.read_csv('/home/Desktop/dates.csv')
         df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)


       df['year'], df['month'], df['day'] = df['Date'].dt.year.map(" 
        {:02}".format), df['Date'].dt.month.map("{:02}".format), 
        df['Date'].dt.day.map("{:02}".format)

       df['year2']=df['Date'].dt.strftime('%y')


      for p in np.arange(len(df)):
              year=str(df.iloc[p,1])
              month=str(df.iloc[p,2])
              day=str(df.iloc[p,3])
              year2=str(df.iloc[p,4])

              os.chdir('/home/Documents/PhD/Data/'+year+'/DWD/' 
              +month+ '/')       
              for file in glob.glob('hg' +year2+ month+ day +'*.nc'):
                 data1 = Dataset(file, 'r')
                 data1.set_auto_mask(False)
                 hour= str(file[8:12])
          
             errorbc = data1.variables['ErrorBackscatter'][:]
             bc = data1.variables['Backscatter'][:
             alt = data1.variables['Altitude'][:]
                      
             df_final = pd.DataFrame({'bc': bc, 'bc_e': errorbc, 
             'hour':hour}, index=alt)
             df_final = df_final.assign(date=df.iloc[p].Date)

hg1309201100.nc

hg1309201200.nc

hg1310281300.nc

我写的剧本每天只读一个小时……如果一天有2到3个不同的小时,它只是忽略了它。例如,对于8月25日,它的读数仅为hg1308250200.nc

 df=pd.read_csv('/home/Desktop/dates.csv')
         df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)


       df['year'], df['month'], df['day'] = df['Date'].dt.year.map(" 
        {:02}".format), df['Date'].dt.month.map("{:02}".format), 
        df['Date'].dt.day.map("{:02}".format)

       df['year2']=df['Date'].dt.strftime('%y')


      for p in np.arange(len(df)):
              year=str(df.iloc[p,1])
              month=str(df.iloc[p,2])
              day=str(df.iloc[p,3])
              year2=str(df.iloc[p,4])

              os.chdir('/home/Documents/PhD/Data/'+year+'/DWD/' 
              +month+ '/')       
              for file in glob.glob('hg' +year2+ month+ day +'*.nc'):
                 data1 = Dataset(file, 'r')
                 data1.set_auto_mask(False)
                 hour= str(file[8:12])
          
             errorbc = data1.variables['ErrorBackscatter'][:]
             bc = data1.variables['Backscatter'][:
             alt = data1.variables['Altitude'][:]
                      
             df_final = pd.DataFrame({'bc': bc, 'bc_e': errorbc, 
             'hour':hour}, index=alt)
             df_final = df_final.assign(date=df.iloc[p].Date)

有人知道如何解决这个问题,以便在数据帧中获取同一天的所有文件吗?

您不一致的空格使代码有点难以读取,但看起来最后6行代码在
for
循环之外,但使用了内部变量。在这种情况下,他们将使用最后分配的值,根据您的示例,8月25日的小时为“0200”

要解决这个问题,请在for循环内部创建用于
df_final
的字典,并将其保存到列表中,然后在外部执行最终连接。


您能否向我们展示您的样本数据和预期输出?我们可以帮助你,看看你的代码是否最优。我在这里得到的答案帮助很大!正是我要找的!非常感谢。