Session Ready
Exercise

Reading a flat file

In previous exercises, we have preloaded the data for you using the pandas function read_csv(). Now, it's your turn! Your job is to read the World Bank population data you saw earlier into a DataFrame using read_csv(). The file is available in the variable data_file.

The next step is to reread the same file, but simultaneously rename the columns using the names keyword input parameter, set equal to a list of new column labels. You will also need to set header=0 to rename the column labels.

Finish up by inspecting the result with df.head() and df.info() in the IPython Shell (changing df to the name of your DataFrame variable).

pandas has already been imported and is available in the workspace as pd.

Instructions
100 XP
  • Use pd.read_csv() with the string data_file to read the CSV file into a DataFrame and assign it to df1.
  • Create a list of new column labels - 'year', 'population' - and assign it to the variable new_labels.
  • Reread the same file, again using pd.read_csv(), but this time, add the keyword arguments header=0 and names=new_labels. Assign the resulting DataFrame to df2.
  • Print both the df1 and df2 DataFrames to see the change in column names. This has already been done for you.