How to write a python code to create the following dataframe empdf from a dictionary?

0 votes
asked by about Advanced IP Scanner
edited by

(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.

1 Answer

0 votes
answered by (90.5k points)

The code to create the DataFrame and perform the requested operations is as follows:

```python
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

highsalarydf = empdf[empdf['Salary'] > 20000]
print(highsalarydf[['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.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register
...