从github文件夹-Python-COVID-19导入多个csv文件

从github文件夹-Python-COVID-19导入多个csv文件,python,pandas,csv,Python,Pandas,Csv,我在这里尝试做两件事: 导入所有.csv文件并将其添加到df 使用上载的最新文件更新df 我已经能够通过以下方式导入一个.csv文件: import pandas as pd url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-22-2020.csv' pd.read_csv(url).fillna(0)

我在这里尝试做两件事:

  • 导入所有.csv文件并将其添加到df
  • 使用上载的最新文件更新df
  • 我已经能够通过以下方式导入一个.csv文件:

    import pandas as pd
    url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-22-2020.csv' 
    pd.read_csv(url).fillna(0)
    
    我可以逐个导入所有
    .csv
    文件(如果我知道如何提取所有
    .csv
    文件名,还可以使用循环),但应该有一种更有效的方法。一旦我有了df,要“更新”它,我会:

  • 提取所有
    .csv
    文件名
  • 检查它们是否都在df中(带有日期列)。如果缺少一个,请将缺少的.csv文件添加到df

  • 我遇到的问题是:(a)如何通过提取所有.csv文件的方式使其具有可伸缩性?(b)有没有办法从github文件夹中只提取以
    .csv
    结尾的文件名?要执行上述第(2)项操作。

    您可以通过以下方式列出所有
    csv
    文件:

    import glob
    
    csvfiles = glob.glob("/path/to/foder/*.csv")
    

    在您拥有了所有的
    csv
    文件路径后,现在您可以在其上循环并将其读取到
    df
    ,检查是否缺少列或任何其他类型的您可以通过以下方式列出所有
    csv
    文件:

    import glob
    
    csvfiles = glob.glob("/path/to/foder/*.csv")
    
    在您拥有了所有的
    csv
    文件路径后,现在您可以在其上循环并将其读取到
    df
    ,检查列是否丢失或任何其他类型

    我建议您使用IMHO,因为它提供了一种更简单的处理文件的方法:

    from pathlib import Path 
    files = Path(folder containing files)
    #filter for only csv files
    csv_only = files.rglob('*.csv')
    #read ur csv files using a list comprehension
    #u can attach the filename if it is relevant and makes sense
    #by using the stem function from pathlib
    combo = [pd.read_csv(f)
             .assign(f.stem)
             .fillna(0)
             for f in csv_only]
    
    #u can lump them all into one dataframe, using pandas' concat function:
    
     one_df = pd.concat(combo,ignore_index=True)
    
    #u can remove duplicates :
    
    one_df = one_df.drop_duplicates('date')
    
    我建议您使用IMHO,因为它提供了一种更容易处理文件的方法:

    from pathlib import Path 
    files = Path(folder containing files)
    #filter for only csv files
    csv_only = files.rglob('*.csv')
    #read ur csv files using a list comprehension
    #u can attach the filename if it is relevant and makes sense
    #by using the stem function from pathlib
    combo = [pd.read_csv(f)
             .assign(f.stem)
             .fillna(0)
             for f in csv_only]
    
    #u can lump them all into one dataframe, using pandas' concat function:
    
     one_df = pd.concat(combo,ignore_index=True)
    
    #u can remove duplicates :
    
    one_df = one_df.drop_duplicates('date')
    

    我仍在努力寻找更好的解决方案,但下面是一个变通方法,我使用我的代码从github目录中提取。不幸的是,我仍然没有找到一种方法,在github directoy中只获取CSV列表,就像在本地驱动器上一样

    def read_multi_csv(start_year,end_year):     
        years = list(range(start_year,end_year+1))     
        dfs = []
        for YYYY in years:         
            file = 'https://raw.githubusercontent.com/username/project/main/data/normalized/'+str(YYYY)+'_crimes_byState.csv'             
            #print (file)         
            df = pd.read_csv(file)         
            dfs.append(df)
        all_dfs = df.concat(df)         
        return all_dfs  
    
    read_multi_csv(2013,2019)
    

    我仍在努力寻找更好的解决方案,但下面是一个变通方法,我使用我的代码从github目录中提取。不幸的是,我仍然没有找到一种方法,在github directoy中只获取CSV列表,就像在本地驱动器上一样

    def read_multi_csv(start_year,end_year):     
        years = list(range(start_year,end_year+1))     
        dfs = []
        for YYYY in years:         
            file = 'https://raw.githubusercontent.com/username/project/main/data/normalized/'+str(YYYY)+'_crimes_byState.csv'             
            #print (file)         
            df = pd.read_csv(file)         
            dfs.append(df)
        all_dfs = df.concat(df)         
        return all_dfs  
    
    read_multi_csv(2013,2019)
    

    这回答了你的问题吗?不,因为文件在网络上。主要问题是从web文件夹中提取以
    .csv
    结尾的文件名。下面所有的解决方案都假设文件夹在网络中时是本地的。我也在试图解决这个谜团。这是否回答了你的问题?不,因为文件在网络上。主要问题是从web文件夹中提取以
    .csv
    结尾的文件名。下面所有的解决方案都假设文件夹在web上时是本地的。我也在试图解决这个谜团。这个答案似乎不起作用。我只是得到一个空列表,而不是我需要的.csv文件的名称。@Geonerd,因为你给出的路径没有csv文件:)这个答案似乎不起作用。我只是得到一个空列表,而不是我需要的.csv文件名。@Geonerd,因为您给出的路径没有csv文件:)