Informatics Practices
Create two dataframes of salary of five employees and do the following:
(a) Display both the dataframes.
(b) Add 5000 as bonus in both dataframes and display them.
Answer
(a)
import pandas as pd
data1 = {'Employee': ['Jatin', 'Avinash', 'Kavya', 'Apoorva', 'Nitin'],
'Salary': [50000, 60000, 70000, 80000, 90000]}
df1 = pd.DataFrame(data1)
data2 = {'Employee': ['Saanvi', 'Aditi', 'Shashank', 'Swapnil', 'Shravani'],
'Salary': [55000, 45000, 30000, 85000, 66000]}
df2 = pd.DataFrame(data2)
print("DataFrame 1:")
print(df1)
print("\nDataFrame 2:")
print(df2)
Output
DataFrame 1:
Employee Salary
0 Jatin 50000
1 Avinash 60000
2 Kavya 70000
3 Apoorva 80000
4 Nitin 90000
DataFrame 2:
Employee Salary
0 Saanvi 55000
1 Aditi 45000
2 Shashank 30000
3 Swapnil 85000
4 Shravani 66000
(b)
df1['Bonus'] = 5000
df2['Bonus'] = 5000
print("DataFrame 1 with bonus:")
print(df1)
print("\nDataFrame 2 with bonus:")
print(df2)
Output
DataFrame 1 with bonus:
Employee Salary Bonus
0 Jatin 50000 5000
1 Avinash 60000 5000
2 Kavya 70000 5000
3 Apoorva 80000 5000
4 Nitin 90000 5000
DataFrame 2 with bonus:
Employee Salary Bonus
0 Saanvi 55000 5000
1 Aditi 45000 5000
2 Shashank 30000 5000
3 Swapnil 85000 5000
4 Shravani 66000 5000
Related Questions
Create a dataframe ‘Student’ from two series—Name and Grade, Name and Marks of five students.
(a) Display the first three records from student dataframe.
(b) Display the last two records from student dataframe.
Create a dataframe of dictionary consisting of Name, Sub1, Sub2, Sub3, Sub4, Sub5 of five students.
(a) Display the dataframe.
(b) Display the first 5 rows and bottom 3 rows of student dataframe.
Create a dataframe using list [10, 11, 12, 13, 14] [23, 34, 45, 32, 65] [55, 60, 65, 70, 75] and do the following:
(a) Display the dataframe.
(b) Add the list [1, 2, 3, 4, 5] to dataframe and display it.
Create a dataframe of [23, 25], [34], [43, 44, 45, 46] and do the following: :
(a) Display the dataframe. Notice that the missing value is represented by NaN.
(b) Replace the missing value with 0.
(c) Replace the missing value with -1, -2, -3, -4 for columns 0, 1, 2, 3.
(d) Replace the missing value by copying the value from the above cell.