This commit is contained in:
2024-09-13 17:47:19 +02:00
parent f4de2667da
commit 64ba3fe295
3 changed files with 36 additions and 2 deletions

View File

View File

@ -16,13 +16,47 @@ 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
cars = pd.read_csv('cars.csv')
print(pathofcars)
cars = pd.read_csv(pathofcars)
# Print out cars
print(cars)
#https://www.learnpython.org/en/Pandas_Basics
# 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)