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
67 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 25 by Omkar
• 67,140 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,160 points
622 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,160 points
31 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
3,684 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 76 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 134 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
• 67,140 points
119 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
• 67,140 points

edited Aug 8, 2018 by Omkar 400 views