LoslegenKostenlos loslegen

Work with multiple spreadsheets

Workbooks meant primarily for human readers, not machines, may store data about a single subject across multiple sheets. For example, a file may have a different sheet of transactions for each region or year in which a business operated.

The FreeCodeCamp New Developer Survey file is set up similarly, with samples of responses from different years in different sheets. Your task here is to compile them in one dataframe for analysis.

pandas has been imported as pd. All sheets have been read into the ordered dictionary responses, where sheet names are keys and dataframes are values, so you can get dataframes with the values() method.

Diese Übung ist Teil des Kurses

Streamlined Data Ingestion with pandas

Kurs anzeigen

Anleitung zur Übung

  • Create an empty dataframe, all_responses.
  • Set up a for loop to iterate through the values in the responses dictionary.
  • Concatenate each dataframe to all_responses and reassign the result to the same variable name.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Create an empty dataframe
all_responses = ____

# Set up for loop to iterate through values in responses
for df in ____:
  # Print the number of rows being added
  print("Adding {} rows".format(df.shape[0]))
  # Concatenate all_responses and df, assign result
  all_responses = pd.concat(____)

# Graph employment statuses in sample
counts = all_responses.groupby("EmploymentStatus").EmploymentStatus.count()
counts.plot.barh()
plt.show()
Code bearbeiten und ausführen