[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

Panda3c.ipynb - Colab

The document provides examples of various DataFrame functions in Python's pandas library, including apply, assign, sort, and merge. It demonstrates how to perform operations such as calculating square roots, summing columns and rows, adding new columns, sorting data, and merging DataFrames. Each function is illustrated with sample data and corresponding outputs.
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)
9 views3 pages

Panda3c.ipynb - Colab

The document provides examples of various DataFrame functions in Python's pandas library, including apply, assign, sort, and merge. It demonstrates how to perform operations such as calculating square roots, summing columns and rows, adding new columns, sorting data, and merging DataFrames. Each function is illustrated with sample data and corresponding outputs.
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/ 3

1/31/25, 10:06 AM 149_panda3c.

ipynb - Colab

import pandas as pd
import numpy as np
#DataFrame.Apply function
print("DataFrame.apply function:\n")
info=pd.DataFrame([[2,7]]*3,columns=['P','Q'])
print("\nOriginalDataFrame:\n",info)
print("\nSquare root of DataFrame:\n",info.apply(np.sqrt))
print("\nSum of each column:\n",info.apply(np.sum,axis=0))
print("\nSum of each row:\n",info.apply(np.sum,axis=1))

DataFrame.apply function:

OriginalDataFrame:
P Q
0 2 7
1 2 7
2 2 7

Square root of DataFrame:


P Q
0 1.414214 2.645751
1 1.414214 2.645751
2 1.414214 2.645751

Sum of each column:


P 6
Q 21
dtype: int64

Sum of each row:


0 9
1 9
2 9
dtype: int64

info=pd.DataFrame([[1,5,7],[2,7,8],[3,6,9]],columns=['X','Y','Z'])
print("\nOriginalDataFrame:\n",info)
print("\nMinimum and Maximum of each column:\n")
print(info.agg(['min','max']))

OriginalDataFrame:
X Y Z
0 1 5 7
1 2 7 8
2 3 6 9

Minimum and Maximum of each column:

X Y Z
min 1 5 7
max 3 7 9

#DataFrame.Assign function
print("\nDataFrame.Assign function:\n")
d2=pd.DataFrame([['Dale',123],['Mark',143]],columns=['Emp','ID'])

https://colab.research.google.com/drive/11pFRHWAE96Kth8gng91FbAXV5t18NJzU#scrollTo=YxHpb45ugZ6z&printMode=true 1/3
1/31/25, 10:06 AM 149_panda3c.ipynb - Colab

print("\nOriginalDataFrame:\n",d2)
d2['Age']=[35,40]
print("\nAdding new column:\n",d2)
d=d2.assign(Sex=['Male','Male'])
print("\nAdding new column:\n",d)

DataFrame.Assign function:

OriginalDataFrame:
Emp ID
0 Dale 123
1 Mark 143

Adding new column:


Emp ID Age
0 Dale 123 35
1 Mark 143 40

Adding new column:


Emp ID Age Sex
0 Dale 123 35 Male
1 Mark 143 40 Male

#DataFrame.Sort functtion
print("\nDataFrame.Sort function:\n")
info=pd.DataFrame(np.random.randn(5,2),index=[3,2,0,4,1],columns=['col3','col4'])
print(info)
info2=info.sort_index()
print("\nSort index:\n",info2)
#info=pd.DataFrame({'col1':[7,1,8,3],'col2':[8,12,4,9]})
info3=info.sort_values(by='col3')
print("\nSort Values:\n",info3)

DataFrame.Sort function:

col3 col4
3 0.951809 0.520526
2 -0.861940 -0.610026
0 -0.267300 0.607471
4 -1.514279 0.943347
1 0.332346 -1.172509

Sort index:
col3 col4
0 -0.267300 0.607471
1 0.332346 -1.172509
2 -0.861940 -0.610026
3 0.951809 0.520526
4 -1.514279 0.943347

Sort Values:
col3 col4
4 -1.514279 0.943347
2 -0.861940 -0.610026
0 -0.267300 0.607471
1 0.332346 -1.172509
3 0.951809 0.520526
https://colab.research.google.com/drive/11pFRHWAE96Kth8gng91FbAXV5t18NJzU#scrollTo=YxHpb45ugZ6z&printMode=true 2/3
1/31/25, 10:06 AM 149_panda3c.ipynb - Colab

#DataFrame.Merge function
print("\nDataFrame.Merge function:\n")
left=pd.DataFrame({
'id':[1,2,3,4,5],
'Name':['Alex','Amy','Allen','Alice','Ayoung'],
'sub':['sub1','sub2','sub4','sub6','sub5']})
right=pd.DataFrame({
'id':[1,2,3,4,5],
'Name':['Billy','Brian','Bran','Bryce','Betty'],
'sub':['sub2','sub4','sub3','sub6','sub5']})
print(left)
print(right)
print(pd.merge(left,right,on='id'))

DataFrame.Merge function:

id Name sub
0 1 Alex sub1
1 2 Amy sub2
2 3 Allen sub4
3 4 Alice sub6
4 5 Ayoung sub5
id Name sub
0 1 Billy sub2
1 2 Brian sub4
2 3 Bran sub3
3 4 Bryce sub6
4 5 Betty sub5
id Name_x sub_x Name_y sub_y
0 1 Alex sub1 Billy sub2
1 2 Amy sub2 Brian sub4
2 3 Allen sub4 Bran sub3
3 4 Alice sub6 Bryce sub6
4 5 Ayoung sub5 Betty sub5

https://colab.research.google.com/drive/11pFRHWAE96Kth8gng91FbAXV5t18NJzU#scrollTo=YxHpb45ugZ6z&printMode=true 3/3

You might also like