Dołączanie ramek danych generowanych w pętli for

Uzyskuję dostęp do serii plików Excela w pętli for. Następnie odczytuję dane z pliku excel do ramki danych pandy. Nie mogę dowiedzieć się, jak dołączyć te ramki danych razem, aby następnie zapisać ramkę danych (teraz zawierającą dane ze wszystkich plików) jako nowy plik Excel.

Oto co próbowałem:

for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    appended_data = pandas.DataFrame.append(data) # requires at least two arguments
appended_data.to_excel("appended.xlsx")
Dzięki!
Author: El Confuso, 2015-02-23

2 answers

Użycie pd.concat Aby połączyć listę ramek danych w jedną dużą ramkę danych.

appended_data = []
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    # store DataFrame in list
    appended_data.append(data)
# see pd.concat documentation for more info
appended_data = pd.concat(appended_data, axis=1)
# write DataFrame to an excel sheet 
appended_data.to_excel('appended.xlsx')
 67
Author: biobirdman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-20 08:27:39

Możesz spróbować tego.

data_you_need=pd.DataFrame()
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    data_you_need=data_you_need.append(data,ignore_index=True)
Mam nadzieję, że to pomoże.
 16
Author: ye jiawei,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-24 02:01:14