Tutorials
python
+1

pandas to CSV

Learn how to import data from and export data to CSV files using pandas.

You work for an airline, and your manager has asked you to do a competitive analysis and see how often passengers flying on other airlines are involuntarily bumped from their flights. You got a CSV file (airline_bumping.csv) from the Department of Transportation containing data on passengers that were involuntarily denied boarding in 2016 and 2017, but it doesn't have the exact numbers you want. In order to figure this out, you'll need to get the CSV into a pandas DataFrame and do some manipulation!

pandas is imported for you as pd. "airline_bumping.csv" is in your working directory.

Exercise

TRY IT YOURSELF: Access the exercise in our Data Manipulation with pandas course here.

  • Read the CSV file "airline_bumping.csv" and store it as a DataFrame called airline_bumping.
  • Print the first few rows of airline_bumping.
# Read CSV as DataFrame called airline_bumping
airline_bumping = ____

# Take a look at the DataFrame
print(____)

You're almost there! To make things easier to read, you'll need to sort the data and export it to CSV so that your colleagues can read it.

  • Sort airline_totals by the values of bumps_per_10k from highest to lowest, storing as airline_totals_sorted.
  • Print your sorted DataFrame.
  • Save the sorted DataFrame as a CSV called "airline_totals_sorted.csv".
# Create airline_totals_sorted
airline_totals_sorted = ____

# Print airline_totals_sorted
____

# Save as airline_totals_sorted.csv
____

Video

To learn more about reading and writing CSVs, please see this video from our course Data Manipulation with pandas.

This content is taken from DataCamp’s Data Manipulation with pandas course by Maggie Matsui.