Day08-Pandas-Tutorial: Pandas - by Punith V T
Day08-Pandas-Tutorial: Pandas - by Punith V T
February 1, 2024
arr = np.arange(1,10)
print("array is : ", arr)
se = pd.Series(arr)
print(se)
array is : [1 2 3 4 5 6 7 8 9]
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
dtype: int64
1
[133]: a 10
b 20
c 30
dtype: int64
"hight" : [120,50,60],
"waight": [20,30,40]
}
df = pd.DataFrame(data)
df
[135]: a b c
0 1 4 7
1 2 5 8
2 3 6 9
df = pd.DataFrame(data)
df
2
3 Aswin Jammu
4 Aswin Punjab
# Creating a DataFrame
df = pd.DataFrame(data)
df
# List of lists
lst = [['id', 'name', 'address'], [1, 2, 3, 4], ['A', 'B', 'C', 'D'], ['Aswin',␣
↪'Anil', 'Akil', 'Aman']]
# Creating a DataFrame
df = pd.DataFrame(dict(zip(lst[0], lst[1:])))
3
df = pd.read_excel(‘sample.xlsx’, sheet_name=‘Sheet1’) print(df)
#export excel
df.to_excel(‘exported_data.xlsx’, sheet_name=‘Sheet1’, index=False)
Dataframe Functions:
[139]: # info() - Provides a concise summary of the DataFrame's structure.
import pandas as pd
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 City 3 non-null object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes
--------------------------------------
Age
count 3.000000
mean 28.333333
std 7.637626
min 20.000000
25% 25.000000
50% 30.000000
75% 32.500000
max 35.000000
[140]: # head() - Displays the first few rows of the DataFrame (default is 5 rows).
4
# Display the first 3 rows
print(df.head(3))
[141]: # tail() - Displays the last few rows of the DataFrame (default is 5 rows).
[142]: # shape - Returns a tuple representing the dimensions of the DataFrame (rows,␣
↪columns).
[142]: (3, 3)
# rows * cols
print(df.size)
9
RangeIndex(start=0, stop=3, step=1)
# column name
print(df.columns)
5
print(df.axes)
df.isna()
[146]: # Transpose of the DataFrame: Convert the row indices to column names and vice␣
↪versa.
df.iloc[1:5,:5].transpose()
[146]: 1 2
Name Manu Ankush
Age 30 35
City Karnataka London
Name Charu
Age 20
City Chennai
Name: 0, dtype: object
0 Charu
1 Manu
2 Ankush
Name: Name, dtype: object
6
Name Age City
1 Manu 30 Karnataka
2 Ankush 35 London
Filtering Data:
[150]: # Filter rows where Age is greater than 30
filtered = df[df['Age'] >= 35]
print(filtered)
7
[154]: Name Age City
1 Manu 50 Karnataka
2 Ankush 35 London
[ ]: