A Gentle Visual Intro to Data Analysis in Python Using Pandas
if you’re planning to learn data analysis, machine learning, or data science tools in python, you’re most likely going to be using the wonderful library. pandas is an open source library for data manipulation and analysis in python.
one of the easiest ways to think about that, is that you can load tables (and excel files) and then slice and dice them in multiple ways:
Pandas allows us to load a spreadsheet and manipulate it programmatically in python. The central concept in pandas is the type of object called a DataFrame – basically a table of values which has a label for each row and column. Let’s load this basic CSV file containing data from a music streaming service:
df = pandas.read_csv('music.csv')
Now the variable
df is a pandas DataFrame:
we can select any column using its label:
we can select one or multiple rows using their numbers:
We can select any slice of the table using a both column label and row numbers using
loc开心时时彩计划软件下载 (but here it would be inclusive of both bounding row numbers):
开心时时彩计划软件下载now it gets more interesting. we can easily filter rows using the values of a specific row. for example, here are our jazz musicians:
here are the artists who have more than 1,800,000 listeners:
Dealing with Missing Values
开心时时彩计划软件下载many datasets you’ll deal with in your data science journey will have missing values. let’s say our data frame has a missing value:
pandas provides multiple ways to deal with this. the easiest is to just drop rows with missing values:
another way would be to fill-in the missing value using (with 0, for example).
开心时时彩计划软件下载things start to get really interesting when you start grouping rows with certain criteria and aggregating their data. for example, let’s group our dataset by genre and see how many listeners and plays each genre has:
Pandas grouped the the two “Jazz” rows into one, and since we used
sum()开心时时彩计划软件下载 for aggregation, it added together the listeners and plays for the two Jazz artists and shows the sums in the combined Jazz column.
This is not only nifty, but is an extremely powerful data analysis method. Now that you know
groupby(), you wield immense power to fold datasets and uncover insights from them. Aggregation is the first , and so is one of the foundational tools of statistics.
In addition to
sum(), pandas provides multiple aggregation functions including
mean() to compute the average value,
max(), and multiple other functions. More on
groupyby() in the .
If you use
groupby() to its full potential, and use nothing else in pandas, then you’d be putting pandas to great use. But the library can still offer you much, much more.
Creating New Columns from Existing Columns
often in the data analysis process, we find ourselves needing to create new columns from existing ones. pandas makes this a breeze.
by telling pandas to divide a column by another column, it realizes that we want to do is divide the individual values respectively (i.e. each row’s “plays” value by that row’s “listeners” value).
Get Hands On!
you can get started playing with pandas in your browser right now through this basic . the notebook is also if you have your local environment set up.
Learn More Pandas
want to learn more? be sure to check out the tutorial in the official pandas docs. thanks to for initiating the thoughts for these visualizations and continuing to improve the pandas documentation.
Your feedback is appreciated!
did you find this tutorial helpful? any suggestions for improvement? please let me know () know on twitter. thanks!