0% found this document useful (0 votes)
16 views8 pages

Day08-Pandas-Tutorial: Pandas - by Punith V T

Uploaded by

Luka Filipovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Day08-Pandas-Tutorial: Pandas - by Punith V T

Uploaded by

Luka Filipovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

day08-pandas-tutorial

February 1, 2024

Pandas –by Punith V T


• Pandas is an open-source data manipulation and analysis library for Python.
• It provides data structures and functions for working with structured data, such as spread-
sheets or SQL tables.
• Pandas offers two primary data structures: Series and DataFrame.
• Series is a one-dimensional array-like object with labels (an index).
• DataFrame is a two-dimensional table with rows and columns.
[132]: #pandas Series
import pandas as pd
import numpy as np

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

[133]: #creating labels


import pandas as pd
a = [10,20,30]
lab = pd.Series(a,index=["a","b","c"])
lab

1
[133]: a 10
b 20
c 30
dtype: int64

[134]: #creating a dataframe


import pandas as pd
data={

"hight" : [120,50,60],
"waight": [20,30,40]
}
df = pd.DataFrame(data)
df

[134]: hight waight


0 120 20
1 50 30
2 60 40

[135]: #using dictionary as in the list


sample= {"a":[1,2,3],
"b":[4,5,6],
"c":[7,8,9]
}
df=pd.DataFrame(sample)
df

[135]: a b c
0 1 4 7
1 2 5 8
2 3 6 9

[136]: #Creating DataFrame from a Dictionary:


import pandas as pd

# Dictionary with mixed data types


data = {'name': 'Aswin', 'city': ['Coimbatore', 'Chennai', 'Hyderabad',␣
↪'Jammu', 'Punjab']}

df = pd.DataFrame(data)
df

[136]: name city


0 Aswin Coimbatore
1 Aswin Chennai
2 Aswin Hyderabad

2
3 Aswin Jammu
4 Aswin Punjab

[137]: import numpy as np


a = np.array([1, 2, 3, 4])
b = np.array(['A', 'B', 'C', 'D'])
c = np.array(['Kop', 'San', 'Sat', 'Pune'])

# Dictionary of NumPy arrays


data = {'id': a, 'name': b, 'address': c}

# Creating a DataFrame
df = pd.DataFrame(data)
df

[137]: id name address


0 1 A Kop
1 2 B San
2 3 C Sat
3 4 D Pune

[138]: import pandas as pd

# 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:])))

# Displaying the DataFrame


df

[138]: id name address


0 1 A Aswin
1 2 B Anil
2 3 C Akil
3 4 D Aman

Importing and Exporting CSV Files:


#import csv
import pandas as pd df = pd.read_csv(‘sample.csv’) print(df)
#export csv
df.to_csv(‘exported_data.csv’, index=False)
#import excel

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

# Create a sample DataFrame


data = {'Name': ['Charu', 'Manu', 'Ankush'],
'Age': [20, 30, 35],
'City': ['Chennai', 'Karnataka', 'London']}
df = pd.DataFrame(data)

# Display DataFrame information


df.info()
print("--------------------------------------")
# Generate summary statistics
summary = df.describe()

# Display the summary statistics


print(summary)

<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))

Name Age City


0 Charu 20 Chennai
1 Manu 30 Karnataka
2 Ankush 35 London

[141]: # tail() - Displays the last few rows of the DataFrame (default is 5 rows).

# Display the last 2 rows


print(df.tail(2))

Name Age City


1 Manu 30 Karnataka
2 Ankush 35 London

[142]: # shape - Returns a tuple representing the dimensions of the DataFrame (rows,␣
↪columns).

# Get the shape of the DataFrame


shape = df.shape

# Display the shape


print(f"Number of rows: {shape[0]}, Number of columns: {shape[1]}")
shape

Number of rows: 3, Number of columns: 3

[142]: (3, 3)

[143]: # Check size of data frame

# rows * cols
print(df.size)

# Range of index from Start to End


print(df.index)

9
RangeIndex(start=0, stop=3, step=1)

[144]: # Get Names of the Columns.

# column name
print(df.columns)

#Range and Names of the columns

5
print(df.axes)

Index(['Name', 'Age', 'City'], dtype='object')


[RangeIndex(start=0, stop=3, step=1), Index(['Name', 'Age', 'City'],
dtype='object')]

[145]: # isna(): Show null values.

df.isna()

[145]: Name Age City


0 False False False
1 False False False
2 False False False

[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

[147]: # Access the first row by index


first_row = df.loc[0]
print(first_row)

Name Charu
Age 20
City Chennai
Name: 0, dtype: object

[148]: # Access the 'Name' column


names = df['Name']
print(names)

0 Charu
1 Manu
2 Ankush
Name: Name, dtype: object

[149]: # Access rows 1 to 2


rows12 = df.loc[1:2]
print(rows12)

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)

Name Age City


2 Ankush 35 London
Adding and Modifying Data:
[151]: # Add a 'City' column
df['Grade'] = ['A', 'B', 'C']
df

[151]: Name Age City Grade


0 Charu 20 Chennai A
1 Manu 30 Karnataka B
2 Ankush 35 London C

[152]: # Update the 'Age' of the second row


df.at[1, 'Age'] = 50
print(df)

Name Age City Grade


0 Charu 20 Chennai A
1 Manu 50 Karnataka B
2 Ankush 35 London C
Dropping Data:
[153]: # Drop the 'City' column
df = df.drop('Grade', axis=1)
df

[153]: Name Age City


0 Charu 20 Chennai
1 Manu 50 Karnataka
2 Ankush 35 London

[154]: # Drop the first row


df = df.drop(0)
df

7
[154]: Name Age City
1 Manu 50 Karnataka
2 Ankush 35 London

[ ]:

You might also like