Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 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
Quote
#2
from the docs: You can also access the response body as bytes, for non-text requests

you should use text instead

also, I would not use csv name. that is a standard library module and if you need to import and use it in the future you will need to refactor the code in order to avoid conflict
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)