1
#Sort the values of DataFrame
import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,55,32], \
"2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
print(df)
print(df.sort_values(by=["2019"]))
print(df.sort_values(by=["2019"],ascending=True))
print(df.sort_values(by=["2019"],ascending=False))
Output:
2018 2019 2020
IP 50.0 40 65
Bio 60.0 35 75
Chemistry 70.0 45 85
Physics 80.5 55 45
English 90.0 32 52
2018 2019 2020
English 90.0 32 52
Bio 60.0 35 75
IP 50.0 40 65
Chemistry 70.0 45 85
Physics 80.5 55 45
2018 2019 2020
English 90.0 32 52
Bio 60.0 35 75
IP 50.0 40 65
Chemistry 70.0 45 85
Physics 80.5 55 45
2018 2019 2020
Physics 80.5 55 45
Chemistry 70.0 45 85
IP 50.0 40 65
Bio 60.0 35 75
English 90.0 32 52
2.
#Create a DataFrame using Dictionary and default index
import pandas as pd
D={"Name":["Ram","Raj","Sam","John"],"CS":[50,75,55,82], \
"Maths":[40,35,45,55], \
"IP":[65,75,85,45]}
df=pd.DataFrame(D)
print(df)
Name CS Maths IP
0 Ram 50 40 65
1 Raj 75 35 75
2 Sam 55 45 85
3 John 82 55 45
3.
#Create a DataFrame using Dictionary of Series
import pandas as pd
n=pd.Series(["Ram","Raj","Sam","John"])
cs=pd.Series([50,75,55,82])
maths=pd.Series([40,35,45,55])
ip=pd.Series([65,75,85,45])
data={"Name":n,"CS":cs,"Maths":maths,"IP":ip}
df=pd.DataFrame(data)
print(df)
Output:
Name CS Maths IP
0 Ram 50 40 65
1 Raj 75 35 75
2 Sam 55 45 85
3 John 82 55 45
4.
#Create a DataFrame using Dictionary of Series by directly passing the values to the dictionary
import pandas as pd
data={"Name":pd.Series(["Ram","Raj","Sam","John"]),
"CS":pd.Series([50,75,55,82]),
"Maths":pd.Series([40,35,45,55]),
"IP":pd.Series([65,75,85,45])}
df=pd.DataFrame(data)
print(df)
Output:
Name CS Maths IP
0 Ram 50 40 65
1 Raj 75 35 75
2 Sam 55 45 85
3 John 82 55 45
5.
#Create a DataFrame by passing a list of dictionaries
import pandas as pd
data=[{"Ram":45,"Raj":43,"Sam":27,"John":50},
{"Ram":40,"Raj":35,"Sam":45,"John":30},
{"Ram":65,"Raj":75,"Sam":60,"John":55}]
df=pd.DataFrame(data)
print(df)
Output:
Ram Raj Sam John
0 45 43 27 50
1 40 35 45 30
2 65 75 60 55
6.
#Create a DataFrame from numpy ndarray
import pandas as pd
import numpy as np
array=np.array([[67,77,75,78],
[67,78,75,88],
[78,67,89,90],
[78,88,98,90]])
column_values=['English','Economics','IP','Accounts']
df=pd.DataFrame(array,columns=column_values)
print(df)
Output:
English Economics IP Accounts
0 67 77 75 78
1 67 78 75 88
2 78 67 89 90
3 78 88 98 90
7.
#To display records from the first to the third row
import pandas as pd
D={'Name':['ritu','ajay','pankaj','aditya'],'English':[67,78,75,88],'Bio':[78,67,89,90],'Physics':[34,45,67,78],'Chem':[23,32,67,87]}
df=pd.DataFrame(D)
print(df)
print(df[1:3])
Output:
Name English Bio Physics Chem
0 ritu 67 78 34 23
1 ajay 78 67 45 32
2 pankaj 75 89 67 67
3 aditya 88 90 78 87
Name English Bio Physics Chem
1 ajay 78 67 45 32
2 pankaj 75 89 67 67
8.
#To create an indexed DataFrame using list
import pandas as pd
D={'Name':['ritu','ajay','pankaj','aditya'],'English':[67,78,75,88],'Bio':[78,67,89,90],'Physics':[34,45,67,78],'Chem':[23,32,67,87]}
df=pd.DataFrame(D, index=['S1','S2','S3','S4'])
print(df)
print(df[1:3])
Output:
Name English Bio Physics Chem
S1 ritu 67 78 34 23
S2 ajay 78 67 45 32
S3 pankaj 75 89 67 67
S4 aditya 88 90 78 87
9.
#To change the index column (set the DataFrame Column as Index) of DataFrame
import pandas as pd
D={'Name':['ritu','ajay','pankaj','aditya'],'English':[67,78,75,88],'Bio':[78,67,89,90],'Physics':[34,45,67,78],'Chem':[23,32,67,87]}
df=pd.DataFrame(D)
print(df)
df.set_index('Name',inplace=True)
print("New DataFrame:")
print(df)
Output:
==========
Name English Bio Physics Chem
0 ritu 67 78 34 23
1 ajay 78 67 45 32
2 pankaj 75 89 67 67
3 aditya 88 90 78 87
New DataFrame:
English Bio Physics Chem
Name
ritu 67 78 34 23
ajay 78 67 45 32
pankaj 75 89 67 67
aditya 88 90 78 87
10.
Reset the Index column of a DataFrame i.e. Reset to the original DataFrame
import pandas as pd
D={'Name':['ritu','ajay','pankaj','aditya'],'English':[67,78,75,88],'Bio':[78,67,89,90],'Physics':[34,45,67,78],'Chem':[23,32,67,87]}
df=pd.DataFrame(D)
print(df)
df.set_index('Name',inplace=True)
print("New DataFrame:")
print(df)
df.reset_index(inplace=True)
print("Original DataFrame:")
print(df)
Output:
Name English Bio Physics Chem
0 ritu 67 78 34 23
1 ajay 78 67 45 32
2 pankaj 75 89 67 67
3 aditya 88 90 78 87
New DataFrame:
English Bio Physics Chem
Name
ritu 67 78 34 23
ajay 78 67 45 32
pankaj 75 89 67 67
aditya 88 90 78 87
Original DataFrame:
Name English Bio Physics Chem
0 ritu 67 78 34 23
1 ajay 78 67 45 32
2 pankaj 75 89 67 67
3 aditya 88 90 78 87