Confusion in Python Pandas code

0 votes

Can you explain this code to me?

import pandas as pd

data = pd.read_csv("SalaryGender.csv")
df = pd.DataFrame(data, columns=["Salary", "Gender", "Age", "PhD"])
print(df)
selected_data = df.loc[:, ["Age", "PhD"]]
print(selected_data)
i=PhD
for i in selected_data.iter_items():
if i == 0:
del i
I understood the dataframe creation but I am not able to understand the logic. 

Jan 25 in Python by Sukanya
48 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I have added the explanation of code as comments for each line. Please refer it.

import numpy, pandas   # import libraries 

df = pandas.read_csv('SalaryGender.csv', delimiter=',') # reading salarygender data.

salary = numpy.array(df['Salary']) # getting Salary column from pandas data frame and converting into numpy array

gender = numpy.array(df['Gender'] ) # getting Gender column from pandas data frame and converting into numpy array

phd = numpy.array(df['PhD'])  # getting PhD column from pandas data frame and converting into numpy array

age = numpy.array(df['Age'])  # getting Age column from pandas data frame and converting into numpy array


frame = pandas.DataFrame() #creating new data frame with Age and PhD column

frame["Age"] = age

frame["PhD"] = phd

# dropping rows where PhD is 0 using for loop

for i in range(0, 100):

    if frame.loc[i]["PhD"] == 0:

        frame = frame.drop(i)


print(frame) # Printing data frame after dropping rows where PhD is 0
answered Jan 25 by Omkar
• 65,810 points

Related Questions In Python

0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,100 points
273 views
0 votes
1 answer

How to Convert usual text to executable machine code in python

what you are looking for is a ...READ MORE

answered Oct 1, 2018 in Python by Priyaj
• 56,100 points
21 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,500 points
2,323 views
0 votes
1 answer

How to use Pandas HDF5 as a Database in Python?

HDF5 works fine for concurrent read only ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,500 points

edited Dec 10, 2018 by Nymeria 47 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points
18 views
+4 votes
6 answers
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 65,810 points
36 views
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 65,810 points

edited Aug 8, 2018 by Omkar 266 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.