Search
CSV Files and Data Frames

This exercise should get you started with handling data in Python. There is no data analysis yet, just some loading and filtering of the data.

Loading data from CSV

Your first step is always accessing the data. Often, the data is stored in a database or within files. One common and generic exchange format for files are Comma Separated Value (CSV) files. The first line of such a file indicates the names of the features, the following lines each contain a single instance.

First, download the bankruptcy data set we prepared for you and and upload it to your Jupyter notebook. Please note, that we slightly modified the data from the original available in the UCI archive UCI for this exercise, e.g., to include missing values.

Use the cell below to load the data from the CSV file. The data should be loaded into a data frame. Data frames are available in python using the pandas library. In comparison to matrices or similar types, they allow different types of columns, are usually easier to manipulate, e.g., by adding or removing rows/columns, and rows and columns can be named.

Once you have done this, print some information about the data:

  • number of instances
  • number of features
  • names of the features

You should have 55 instances with 7 features.

Remove features

If you load all data from a file, you often also load irrelevant features for a task. In case of the data you just loaded, the feature is called Company. This is an ID feature for the instances in the data. Such data must often be removed before further analysis of the data. Your second task is to remove this feature from the data.

Remove instances with missing values

Real-life data is often not clean, i.e., the data has many problems which must be addressed first, before it can be used for analysis. One common problem are missing features, i.e., not all features are available for all data. This is also the case for the data you just loaded. All missing values are marked as NA in the CSV file.

Your third task is to remove all instances from the data, that have any missing values and store the remaining instances in a new data frame. If this works correctly, five instances should be removed. You can check this, e.g., by comparing the sizes of the data frames or printing the instances that were removed.

Computing with data frames

Sometimes you have to compute new columns from the values of existing columns. Please append two new columns to the data frame: The sum of the columns WC/TA and RE/TA and the product of the columns EBIT/TA and S/TA.

Merging data frames

The next task of this exercise is to merge data frames. For this, load the data from the same CSV file as above again. Then merge the data frame with the result from task 2.4, such that:

  • the dropped feature from task 2.2 is part of the merged data frame; and
  • the removed instances from task 2.3 are still gone; and
  • the indirectly computed features from task 2.4 are part of the merged data frame.

Selecting subsets

Based on the data frame from task 2.5, create new data frames according to the following criteria.

  • A data frame with only the rows 10 to 20 and all columns.
  • A data frame with only the columns 1 to 4 and all rows.
  • A data frame with only the columns WC/TA and EBIT/TA and all rows.
  • A data frame with all rows that have the value RE/TA less than -20 and all columns.
  • A data frame with all rows that have the value RE/TA less than -20 and bankrupt equal to 0 and all columns.
  • A data frame with all rows that have the value RE/TA less than -20 and bankrupt equal to 0 and only the columns WC/TA and EBIT/TA.