Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't get the following python code to run without errors. Can you please tell me what the problem is and how I would fix

I can't get the following python code to run without errors. Can you please tell me what the problem is and how I would fix it?

Thank you!

Here is the code:

import pandas as pd titanic=pd.read_csv('/Users/user/Desktop/train.csv') titanic

titanic[pd.isnull(titanic.Cabin)] titanic[pd.isnull(titanic.Cabin)].PassengerId len(titanic[pd.isnull(titanic.Cabin)]) titanic['Cabin'].fillna(0)

titanic['Age']=titanic['Age'].fillna(titanic['Age'].median())

import random as rnd

titanic=pd.read_csv('/Users/user/Desktop/train.csv') ageMean=titanic.Age.mean() ageStd=titanic.Age.std() titanic.Age.fillna(int(rnd.uniform(ageMean-ageStd, ageMean+ageStd)))

titanic.Embarked.unique() titanic['Embarked']=titanic['Embarked'].fillna('S') titanic.loc[titanic['Embarked']=='S', 'Embarked' ]=0 titanic.loc[titanic['Embarked']=='C', 'Embarked' ]=1 titanic.loc[titanic['Embarked']=='Q', 'Embarked' ]=2

titanic.Sex.unique() titanic.loc[titanic['Sex']=='male', 'Sex' ]=0 titanic.loc[titanic['Sex']=='female', 'Sex' ]=1

titanic.Cabin[:]=list(titanic.Cabin[:]) def cabin_abbreviation(df): df.Cabin=df.Cabin.fillna('N') df.CabinAbbrev=df.Cabin.apply(lambda x: x[0]) return df titanic=cabin_abbreviation(titanic) titanic.CabinAbbrev[0:3]

import re

def get_titles(name): title_search=re.search(' ([A-Za-z]+)\.', name) if title_search: return title_search.group(1) return "" titles=titanic.Name.apply(get_titles)

titles[0:3] pd.value_counts(titles)

title_mapping={"Mr":1, "Miss":2, "Mrs":3, "Master":4, "Dr":5, "Rev":6, "Col":7, "Mlle":8, "Mme":9, "Mlle":8, "Countess":10, "Lady":10, "Jonkheer":10, "Sir":9, "Capt":7, "Ms":2} for k,v in title_mapping.items(): titles[titles==k]=v titles[0:3]

pd.value_counts(titles)

titanic['Title']=titles

def extract_titles(df): df.LastName=df.Name.apply(lambda x: x.split(' ')[0]) df.Title=df.Name.apply(lambda x: x.split(' ')[1]) return df

titanic.Name[0] titanic.Title[1]

dtitanic_NoOutlierAgeRecords1=titanic[np.abs(titanic.Age-titanic.Age.mean)]

titanic_wNoOutlierAgeRecords2=titanic[~(np.abs(titanic.Age-titanic.Age.mean))]

def age_binning(df): df.Age.fillna(-0.5) thresholds=[-1, 0, 5, 12, 18, 60, 120]

Here is the train.csv file if you need it:

https://www.dropbox.com/s/6invllojk2w4lnx/train.csv?dl=0

image text in transcribed

1 import pandas as pd 2 titanic-pd.read csv/Users/schwe/Desktop/train.csv) 3 titanic Usage S titanicpd.isnull(titanic.Cabin)) 6 titanicpd.isnull(titanic.Cabin)].PassengerId 7 len(titanic[pd.isnull(titanic.Cabin)]) Here you can get help of any object by pressing Ctrl+I in front of it, either on the Editor or the Console B titanic[ Cabin'.illa(e) 1 titani A-titanic Age.fillna(titanic Age'].median)) 12 import random as rnd Help can also be shown automatically after writing a left parenthesis next to an object. You can activate this behavior in Preferences > Help 14 titanic.pd.read-c sv(Users/schwe/Desktop/train.csv.) 15 ageMean-titanic.Age.mean) 16 ageStd-titanic.Age.std) 17 titanic.Age.fillna(int (rnd.uniform(ageMean-ageStd, ageMeantageStd))) New to Spyder? Read our tutorial 19 titanic.Embarked.unique) 2 titanic Embarked]-titanicl Embarked'].fillna'S) 21 titanic, lot[titanic['Embarked.].2'S', 'Embarked, jae 22 titanicloc[titanicl Embarked']CEmbarked' ]-1 23 titanic.loc[titanicl Embarked']Q Embarked' 1-2 25 titanic.Sex.unique) 26 titanic.loc[titanicl Sex'male, 'Sex-e 27 titanic, lot[titanic('sex'' female', .Sex J-1 29 titanic.Cabin:-list (titanic.Cabin:]) 3e def cabin abbreviation(df): 31 df.Cabin-df.Cabin.fillna'N) 32 df.CabinAbbrev-df.Cabin.apply(lambda x: xe]) Variable explorer File explorer Help return df 34 titanic-cabin abbreviation(titanic) 35 titanic.CabinAbbrev[:3) Console 3/A 36 37 import re 38 39 def get_titles (name): 4e title search-re.search(A-Za-2+), name) In [13]: runfile('C:/Users/schwe/Lab3.py', wdir- C:/Users/schwe') C:/Users/schwe/Lab3.py: 29: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame if title search: return title search.group(1) 43 44 titles-titanic.Name. apply(get titles) see the caveats in the documentation : http://pandas.pydata .org/pandas-docs/stable/indexing.html#indexing-view-versus-copy titanic.Cabin[:]-list(titanic.Cabin[:]) File "", line 1, in File "C:\Users schwe Anaconda3\liblsite-packages\spyderlutils\sitelsitecustomize.py", 1ine 880, in runfile File "C:\Users schwe Anaconda3\liblsite-packages\spyderlutils\sitelsitecustomize.py", 1ine 102, in execfile File "C:/Users/schwe/Lab3.py", line 69, in File "C:\Users schwe Anaconda3\liblsite-packages pandaslcorelops.py", line 721, in wrapper File "C:\Users schwe Anaconda3\liblsite-packages pandaslcorelops.py", line 682, in safe_na_op File "C:\Users schwe Anaconda3\liblsite-packages pandaslcorelops.py", line 668, in na_op 46 titles [e:3] 47 pd.value counts(titles) 48 9 title mapping-"Mr":1, "Miss":2, "Mrs":3, "Master":4, "Dr" :5, "Rev":6, Traceback (most recent call last): "Col":7, "Mlle":8, "Mme" :9, "Mile":, "Countess" :18 "Lady":1e, "Jonkheer":1e, "Sir":9, "Capt":7, "Ms":2 51 52 for k,v in title mapping.items(): 53 titles[titlesk]-v runfile ('C:/Users/schwe/Lab3.py', wdr= 'C:/Users/schwe ') 5 titles [e:3] 56 execfile(filename, namespace) 57 pd.value counts(titles) 58 59 titanic Title']-titles exec(compile(f.read(), filename, 'exec, namespace) dtitanic NoOutlierAgeRecords1=titanic[np.abs (titanic.Age-titanic . Age.mean)] result = wrap-results(safe-na-op(lvalues, rvalues)) 61 def extract titles (df): 62 df. LastName-df.Name.apply(lambda x: x.split( e]) 63 df.Title-df.Name. apply(1ambda x: x.split1]) return df 65 66 titanic.Name el 67 titanic.itle1] 68 69 dtitanic NoOutlierAgeRecords1-titanicnp.abs(titanic.Age-titanic.Age.mean)) 71 titanic wNoOutlierAgeRecords2-titanicnp.abs(titanic.Age-titanic.Age.mean))) 7e 72 73 def age binning(df): 74 df.Age.fillna(-e.5) return na_op(lvalues, rvalues) A 75 thresholds-l-1, 4, 5, 12, 18, 6e, 120] result [mask] op(X[mask ), y) = TypeError: unsupported operand type(s) for 'float' and method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 15th International Conference Dexa 2004 Zaragoza Spain August 30 September 3 2004 Proceedings Lncs 3180

Authors: Fernando Galindo ,Makoto Takizawa ,Roland Traunmuller

2004th Edition

3540229361, 978-3540229360

More Books

Students also viewed these Databases questions