# Libraries
import pandas as pd
# Dataframe created from a dictionary
data1 = {
"col1":[1,2,3,4,5,6,7,8,9,10],
"col2":[11,12,13,14,15,16,17,18,19,20]
}
df = pd.DataFrame(data = data1)
print(df)
col1 col2
0 1 11
1 2 12
2 3 13
3 4 14
4 5 15
5 6 16
6 7 17
7 8 18
8 9 19
9 10 20
# shape method help us to know the dimension of the df
df.shape
df.shape[0]
df.shape[1]
# row*col
df.size
df.ndim
# Dataframe created from a dictionary
data2 = {
"col1":[1,2,3,4,5,6,7,8],
"col2":[11,12,13,14,15,16,17,18],
"col3":["Car","Bike","Train","Plane","Ship","Aircraft","Mountain Bike","Scooter"],
"col4":[True, False,True, False,True, False,True, False]
}
df = pd.DataFrame(data = data2)
print(df)
col1 col2 col3 col4
0 1 11 Car True
1 2 12 Bike False
2 3 13 Train True
3 4 14 Plane False
4 5 15 Ship True
5 6 16 Aircraft False
6 7 17 Mountain Bike True
7 8 18 Scooter False
print(df.set_index("col3"))
col1 col2 col4
col3
Car 1 11 True
Bike 2 12 False
Train 3 13 True
Plane 4 14 False
Ship 5 15 True
Aircraft 6 16 False
Mountain Bike 7 17 True
Scooter 8 18 False
# the first 5 observations
print(df.head())
# the first 3 observations
#print(df.head(3))
col1 col2 col3 col4
0 1 11 Car True
1 2 12 Bike False
2 3 13 Train True
# the last 5 observations
print(df.tail())
# the last 2 observations
#print(df.tail(2))
col1 col2 col3 col4
3 4 14 Plane False
4 5 15 Ship True
5 6 16 Aircraft False
6 7 17 Mountain Bike True
7 8 18 Scooter False
df1 = df.copy()
print(df1)
col1 col2 col3 col4
0 1 11 Car True
1 2 12 Bike False
2 3 13 Train True
3 4 14 Plane False
4 5 15 Ship True
5 6 16 Aircraft False
6 7 17 Mountain Bike True
7 8 18 Scooter False