63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
# 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 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)
|
|
|
|
#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.
|
|
# Set the index for brics
|
|
brics.index = ["BR", "RU", "IN", "CH", "SA"]
|
|
|
|
# Print out brics with new index values
|
|
print(brics)
|
|
|
|
import os
|
|
pathofcars = os.path.abspath("cars.csv")
|
|
|
|
#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 the cars.csv data: cars
|
|
print(pathofcars)
|
|
cars = pd.read_csv(pathofcars)
|
|
|
|
# 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 will output a Pandas Series, while a double bracket will output a Pandas DataFrame.
|
|
|
|
# Print out country column as Pandas Series
|
|
print(cars['Size'])
|
|
|
|
# Print out country column as Pandas DataFrame
|
|
print(cars[['Model']])
|
|
|
|
# Print out DataFrame with country and drives_right columns
|
|
print(cars[['YEAR', 'Make', 'Size']])
|
|
|
|
# Print out first 4 observations
|
|
print(cars[0:4])
|
|
|
|
# Print out fifth and sixth 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.
|
|
|
|
# Print out observation
|
|
print(cars.iloc[2])
|
|
|
|
# Print out observations
|
|
print(cars.loc[[2,6]])
|
|
|
|
#List every TESLA from the list
|
|
teslas = cars[cars['Make'].values == 'TESLA']
|
|
print(teslas)
|
|
|