Download our e-book of Introduction To Python
Shashank Shanu
2 years ago
# import the pandas library
import pandas as pd
left_df = pd.DataFrame({
'S.No.':[1,2,3,4,5],
'Name': ['Alex', 'Amy', 'Allen',
'Alice', 'Ayoung'],
'subjects':['Maths','Physics','Chemistry','Biology','Civis']})
right_df = pd.DataFrame(
{'S.No.':[1,2,3,4,5],
'Name': ['Billy', 'Brian', 'Bran',
'Bryce', 'Betty'],
'subjects':['Physics','Chemistry','History','Biology','Civis']})
print(left_df)
print(right_df)
S.No. Name subjects
0 1 Alex Maths
1 2 Amy Physics
2 3 Allen Chemistry
3 4 Alice Biology
4 5 Ayoung Civics
S.No. Name subjects
0 1 Billy Physics
1 2 Brian Chemistry
2 3 Bran History
3 4 Bryce Biology
4 5 Betty Civics
# import the pandas library
import pandas as pd
ipl = {'Team name': ['Riders', 'Riders', 'Devils',
'Devils', 'Kings',
'kings',
'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank':
[1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year':
[2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Total
Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df1 = pd.DataFrame(ipl)
grouped = df.groupby('Year')
print(grouped.get_group(2014))
Team Rank Year Points
0 Riders 1 2014 876
2 Devils 2 2014 863
4 Kings 3 2014 741
9 Royals 4 2014 701
import
pandas as pd
first_df
= pd.DataFrame({
'Name': ['Alex', 'Amy', 'Allen',
'Alice', 'Ayoung'],
'subject_id':['sub1','sub2','sub4','sub6','sub5'],
'Marks_scored':[98,90,87,69,78]},
index=[1,2,3,4,5])
second_df
= pd.DataFrame({
'Name': ['Billy', 'Brian', 'Bran',
'Bryce', 'Betty'],
'subject_id':['sub2','sub4','sub3','sub6','sub5'],
'Marks_scored':[89,80,79,97,88]},
index=[1,2,3,4,5])
print(pd.concat([first_df,second_df]))
Name subject_id Marks_scored
1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
1 Billy sub2 89
2 Brian sub4
80
3 Bran sub3 79
4 Bryce sub6 97
5 Betty sub5 88