Learn Pandas Basics Python tabular data manipulation:
Pandas DataFrames
Pandas is a high-level data manipulation tool developed by Wes McKinney. It is built on the Numpy package and its key data structure is called the DataFrame. DataFrames allow you to store and manipulate tabular data in rows of observations and columns of variables.
There are several ways to create a DataFrame. One way is to use a dictionary. For example:
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"], "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"], "area": [8.516, 17.10, 3.286, 9.597, 1.221], "population": [200.4, 143.5, 1252, 1357, 52.98] } import pandas as pd brics = pd.DataFrame(dict) print(brics)
Trinket.io on-line Python compiler
As you can see with the new brics DataFrame, Pandas has assigned a key for each country as the numerical values 0 through 4. If you would like to have different index values, say, the two-letter country code, you can do that easily as well.
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"], "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"], "area": [8.516, 17.10, 3.286, 9.597, 1.221], "population": [200.4, 143.5, 1252, 1357, 52.98] } import pandas as pd brics = pd.DataFrame(dict) # Set the index for brics brics.index = ["BR", "RU", "IN", "CH", "SA"] # Print out brics with new index values print(brics)
Trinket.io on-line Python compiler
Another way to create a DataFrame is by importing a CSV file using Pandas. Now, the CSV cars.csv is stored and can be imported using pd.read_csv:
# Import pandas as pd import pandas as pd # Import the cars.csv data: cars cars = pd.read_csv('cars.csv') # Print out cars print(cars)
Indexing DataFrames
There are several ways to index a Pandas DataFrame. One of the easiest ways to do this is by using square bracket notation.
In the example below, you can use square brackets to select one column of the cars DataFrame. You can either use a single bracket or a double bracket. The single bracket with output a Pandas Series, while a double bracket will output a Pandas DataFrame.
# Import pandas and cars.csv import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out country column as Pandas Series print(cars['cars_per_cap']) # Print out country column as Pandas DataFrame print(cars[['cars_per_cap']]) # Print out DataFrame with country and drives_right columns print(cars[['cars_per_cap', 'country']])
Square brackets can also be used to access observations (rows) from a DataFrame. For example:
# Import cars data import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out first 4 observations print(cars[0:4]) # Print out fifth, sixth, and seventh observation print(cars[4:6])
You can also use loc and iloc to perform just about any data selection operation. loc is label-based, which means that you have to specify rows and columns based on their row and column labels. iloc is integer index based, so you have to specify rows and columns by their integer index like you did in the previous exercise.
# Import cars data import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out observation for Japan print(cars.iloc[2]) # Print out observations for Australia and Egypt print(cars.loc[['AUS', 'EG']])
Related Videos:
Related Posts:
Helping humans speak to the computer
Object-Oriented Programming (OOP)
Python Basic String Operations
Learn about programming Loops in Python
Learn RE – Regular Expressions in Python
The Python Standard Library Ver 3.6.5
Helping humans speak to the computer
Learn Partial functions Python programming
Learn about programming Functions in Python
Learn Multiple Function Arguments in Python