Python Forum
Pandas| iterrows | csv.replace
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas| iterrows | csv.replace
#1
Hello forum,

I wanted to ask you to check what is wrong this piece of code: 

import pandas as pd
import requests
import io

csv = requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data').content
csv = csv.replace(' ', '')                                                                                                ### !!!! 
df = pd.read_csv(io.StringIO(csv.decode('utf-8')), header=None)
df.columns = ['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation', 'relationship',
              'race', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week', 'native-country', 'result']
df = df.drop(['fnlwgt','education-num', 'capital-gain', 'capital-loss','result'], axis=1)

# I want to create a df with all divorced ppl so i type 
Divorced = df[df['martial-status']=="Divorced"]
print(Divorced)                                                                                                            ###   yields an empty df

for index, row in df.iterrows():
     print(row['age'], row['education'])   #
I am using anaconda's spyder for python 3.6. I had no serious issues with the package/spyder before. I would really appreciate if someone could help me with these questions :

1)  csv = csv.replace(' ', '') command doesnt seem to work and gives a following error: 
runfile('G:...')
Traceback (most recent call last):

  File "<ipython-input-27-fbfcdec3f55d>", line 1, in <module>
    runfile('G:....')

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "G:/Study/Masters/UvA Data Science/final exam draft.py", line 7, in <module>
    csv = csv.replace(' ', '')

TypeError: a bytes-like object is required, not 'str'
2) when csv_replace is just commented out, I can not use boolean indexing for some reason! The piece of code which is supposed to create me a df with divorced humans is giving me an empty df!

Any help suggestions appreciated! Thank you for your time.

Cheers,
Max
Reply


Messages In This Thread
Pandas| iterrows | csv.replace - by BeerLover - May-19-2017, 05:39 PM
RE: Pandas| iterrows | csv.replace - by buran - May-19-2017, 05:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas dataframe.replace regex metalray 3 24,807 Feb-24-2017, 12:58 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020