ameet niture
ameet niture

Reputation: 3

'function' object has no attribute 'str' in pandas

I am using below code to read and split the csv file strings separated by /

DATA IS 
SRC_PATH                        TGT_PATH
/users/sn/Retail                /users/am/am
/users/sn/Retail Reports/abc    /users/am/am
/users/sn/Automation            /users/am/am
/users/sn/Nidh                  /users/am/xzy

import pandas as pd

df = pd.read_csv('E:\RCTemplate.csv',index_col=None, header=0)
s1 = df.SRC_PATH.str.split('/', expand=True)

i get the correct split data in s1, but when i am going to do the similar operation on single row it throws error "'function' object has no attribute 'str'"

error is throwing in below code

df2= [(df.SRC_PATH.iloc[0])]
df4=pd.DataFrame([(df.SRC_PATH.iloc[0])],columns = ['first'])
newvar = df4.first.str.split('/', expand=True)

Upvotes: 0

Views: 3916

Answers (1)

G. Anderson
G. Anderson

Reputation: 5955

Pandas thinks you are trying to access the method dataframe.first().

This is why it's best practice to use hard brackets to access dataframe columns rather than .column access

df4['first'].str.split() instead of df4.first.str.split()

Not that this cause common issues with things like a column called 'name' ending up as the name attribute of the dataframe and a host of other problems

Upvotes: 3

Related Questions