(i)Write a Python code to create the following Dataframe Empdf from a Dictionary: (3) EName Salary 0 Kush 10000 1 Ruchika 12000 2 Divya 20000 3 John 25000 (ii) Write python code to display the Ename and Salary having Salary more than 20000. (iii) Write python code to add a column ‘Commission’ with values as 5% of the Salary.
Answer by
Swati Dubey
The code to create the DataFrame and perform the requested operations is as follows:
import pandas as pd # Dictionary for creating DataFrame data = {'EName': ['Kush', 'Ruchika', 'Divya', 'John'], 'Salary': [10000, 12000, 20000, 25000]} # Create DataFrame from dictionary empdf = pd.DataFrame(data) # Print DataFrame print(empdf) # Display EName and Salary with Salary more than 20000 high_salary_df = empdf[empdf['Salary'] > 20000] print(high_salary_df[['EName', 'Salary']]) # Add a column 'Commission' with values as 5% of the Salary empdf['Commission'] = empdf['Salary'] * 0.05 print(empdf)
This code will create the empdf DataFrame, display the EName and Salary for those with salaries greater than 20000, and add a new column named 'Commission' with values calculated as 5% of the Salary.
(i)Write a Python code to create the following Dataframe Empdf from a Dictionary: (3)
EName Salary
0 Kush 10000
1 Ruchika 12000
2 Divya 20000
3 John 25000
(ii) Write python code to display the Ename and Salary having Salary more than 20000.
(iii) Write python code to add a column ‘Commission’ with values as 5% of the Salary.
The code to create the DataFrame and perform the requested operations is as follows:
import pandas as pd
# Dictionary for creating DataFrame
data = {'EName': ['Kush', 'Ruchika', 'Divya', 'John'],
'Salary': [10000, 12000, 20000, 25000]}
# Create DataFrame from dictionary
empdf = pd.DataFrame(data)
# Print DataFrame
print(empdf)
# Display EName and Salary with Salary more than 20000
high_salary_df = empdf[empdf['Salary'] > 20000]
print(high_salary_df[['EName', 'Salary']])
# Add a column 'Commission' with values as 5% of the Salary
empdf['Commission'] = empdf['Salary'] * 0.05
print(empdf)
This code will create the empdf DataFrame, display the EName and Salary for those with salaries greater than 20000, and add a new column named 'Commission' with values calculated as 5% of the Salary.