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 26, 2019 in Python by Sukanya
1,654 views

1 answer to this question.

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 26, 2019 by Omkar
• 69,220 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
• 58,020 points
11,769 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
• 58,020 points
2,653 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,560 points
34,469 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,560 points

edited Dec 10, 2018 by Nymeria 1,342 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,123 views
+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
• 69,220 points
1,733 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 5, 2018 in Python by Omkar
• 69,220 points

edited Aug 8, 2018 by Omkar 8,189 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP