You now know how to load CSV data into Python as pandas dataframes and you also know how to manipulate a dataframe. Let’s now see what data analysis methods we can apply to the pandas dataframes.
You know that the dataframe is the main pandas object. So, if you have a some data loaded in dataframe df, you could apply methods to analyze those data. For instance, here is how you apply the mean method to the dataframe we have been working on:
And you would get:
So, these are the mean values for each of the dataframe columns. Just to remind you, we generated the dataframe in the previous lessons of this tutorial. The dataframe looks like this:
You can get a list of available DataFrame methods using the Python dir function:
And you can get the description of each method using help:
You can also apply methods to columns of the dataframe:
Note though that in this case you are not applying the mean method to a pandas dataframe, but to a pandas series object:
So, checking the type of the object would give the type of the object:
And again you can pass the Series object to the dir method to get a list of available methods.
Adding columns to a DataFrame is quite straightforward:
That would add a new column with label “2014” and the values of the Python list. You can also add a column containing the average income for each state:
And you would get this:
The axis parameter tells Python to compute the mean along axis 1 which means along the columns. Axis set to 0 would go along the rows. Let’s see how to calculate the mean for each year and add them as a new row:
This would add a new row with index “MEAN”:
Instead of throwing an error, pandas generated a NaN datatype for the GEOID column which is a good thing because operations won’t break when the dataframe has non-numeric values.