Získávání dat z více listů

Streamlined Data Ingestion with pandas

Amany Mahfouz

Instructor

Výběr listů k načtení

  • read_excel() načte ve výchozím nastavení první list souboru Excel
  • Pomocí argumentu sheet_name lze načíst jiné listy
  • Listy lze specifikovat názvem nebo pozicí (indexováno od nuly)
  • Předáním seznamu názvů/čísel načtete více listů najednou
  • Všechny argumenty read_excel() se vztahují na všechny načítané listy
Streamlined Data Ingestion with pandas

Výběr listů k načtení

Snímek tabulkového procesoru zobrazující záložky dvou listů

Streamlined Data Ingestion with pandas

Načtení vybraných listů

# Get the second sheet by position index
survey_data_sheet2 = pd.read_excel('fcc_survey.xlsx',
                                   sheet_name=1)

# Get the second sheet by name survey_data_2017 = pd.read_excel('fcc_survey.xlsx', sheet_name='2017')
print(survey_data_sheet2.equals(survey_data_2017))
True
Streamlined Data Ingestion with pandas

Načtení všech listů

  • Předání sheet_name=None do read_excel() načte všechny listy sešitu
survey_responses = pd.read_excel("fcc_survey.xlsx", sheet_name=None)

print(type(survey_responses))
<class 'collections.OrderedDict'>
for key, value in survey_responses.items():
    print(key, type(value))
2016 <class 'pandas.core.frame.DataFrame'>
2017 <class 'pandas.core.frame.DataFrame'>
Streamlined Data Ingestion with pandas

Spojení dohromady

# Create empty dataframe to hold all loaded sheets
all_responses = pd.DataFrame()

# Iterate through dataframes in dictionary for sheet_name, frame in survey_responses.items(): # Add a column so we know which year data is from frame["Year"] = sheet_name
# Add the dataframe to all_responses all_responses = pd.concat([all_responses, frame])
# View years in data print(all_responses.Year.unique())
['2016' '2017']
Streamlined Data Ingestion with pandas

Pojďme si to procvičit!

Streamlined Data Ingestion with pandas

Preparing Video For Download...