Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Visualization: First Steps | Explore Dataset
Introduction to Python for Data Analysis
course content

Kursusindhold

Introduction to Python for Data Analysis

Introduction to Python for Data Analysis

1. Introduction to Python 1/2
2. Introduction to Python 2/2
3. Explore Dataset
4. Becoming an Analyst

book
Visualization: First Steps

An essential tool for Data Analysts is visualization. The first one here is .barplot(). To use the tools you need to import the libraries, look at the syntax:

  • import matplotlib.pyplot as plt
  • import seaborn as sns

We will use the second one, Seaborn, but it is based on Matplotlib, so we need to import two of them. Look at the dataset that we used to use for examples:

Our task is to visualize experience_level and the mean salary for each of them. Look at the code:

12345678
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/INTRO+to+Python/ds_salaries.csv', index_col = 0) df = df[['experience_level', 'salary']].groupby(['experience_level']).median().reset_index() sns.barplot(data = df, x = 'experience_level', y = 'salary') plt.show()
copy

Here, is the output

barplot

Look at the sixth line of code:

df = df[['experience_level', 'salary']].groupby(['experience_level']).median().reset_index()

Here you can recognize the new function .reset_index(). It is easy and just transforms the result of .groupby() function into the regular dataset. Look at the pictures (the first one is before and the second one is after):

Then we will move to the seventh line of code.

sns.barplot(data = df, x = 'experience_level', y = 'salary')

  • sns - referring to seaborn library.
  • barplot the type of plot.
  • data = df the DataFrame.
  • x = 'experience_level' the column for x-axis.
  • y = 'salary' the column for y-axis.

Move to the eighth line of code:

plt.show()

Function from the matplotlib library to output the plot.

Opgave

Swipe to start coding

Visualize the sum of money you receive from users depending on their subscription plan.

  1. Import the seaborn with the sns alias.
  2. Import the matplotlib.pyplot with the plt alias.
  3. Prepare data for visualization using the .groupby() function:
  • Extract columns 'plan', 'price'.
  • Group by column plan.
  • Calculate the sum of all prices for each plan.
  • Reset indices.
  1. Create the barplot using the seaborn:
  • Use df as the data argument
  • Use the 'plan' column for the x-axis
  • Use the 'price' column for the y-axis.
  1. Output the plot.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 16
toggle bottom row

book
Visualization: First Steps

An essential tool for Data Analysts is visualization. The first one here is .barplot(). To use the tools you need to import the libraries, look at the syntax:

  • import matplotlib.pyplot as plt
  • import seaborn as sns

We will use the second one, Seaborn, but it is based on Matplotlib, so we need to import two of them. Look at the dataset that we used to use for examples:

Our task is to visualize experience_level and the mean salary for each of them. Look at the code:

12345678
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/INTRO+to+Python/ds_salaries.csv', index_col = 0) df = df[['experience_level', 'salary']].groupby(['experience_level']).median().reset_index() sns.barplot(data = df, x = 'experience_level', y = 'salary') plt.show()
copy

Here, is the output

barplot

Look at the sixth line of code:

df = df[['experience_level', 'salary']].groupby(['experience_level']).median().reset_index()

Here you can recognize the new function .reset_index(). It is easy and just transforms the result of .groupby() function into the regular dataset. Look at the pictures (the first one is before and the second one is after):

Then we will move to the seventh line of code.

sns.barplot(data = df, x = 'experience_level', y = 'salary')

  • sns - referring to seaborn library.
  • barplot the type of plot.
  • data = df the DataFrame.
  • x = 'experience_level' the column for x-axis.
  • y = 'salary' the column for y-axis.

Move to the eighth line of code:

plt.show()

Function from the matplotlib library to output the plot.

Opgave

Swipe to start coding

Visualize the sum of money you receive from users depending on their subscription plan.

  1. Import the seaborn with the sns alias.
  2. Import the matplotlib.pyplot with the plt alias.
  3. Prepare data for visualization using the .groupby() function:
  • Extract columns 'plan', 'price'.
  • Group by column plan.
  • Calculate the sum of all prices for each plan.
  • Reset indices.
  1. Create the barplot using the seaborn:
  • Use df as the data argument
  • Use the 'plan' column for the x-axis
  • Use the 'price' column for the y-axis.
  1. Output the plot.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 16
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt