Newbee - substing of column - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Newbee - substing of column (/thread-8378.html) |
Newbee - substing of column - andrewjmdata - Feb-17-2018 Hi, I have a column in a dataframe/table called "LSOA Name". 1st issue is the field has a name in it. Referencing it as "df1['LSOA name']" seems to work ok. What I am really trying to do is split the column up as it has a code at the end I don't want. In something like SQL i would code as select substr(['LSOA name'],1,len(['LSOA name'])-4) as LSOA_Area from... Can I do the same thing in Python? I have tried the following - df1['LSOA name'].str.slice(0, len(df1['LSOA name'])-4) This returns the full string, what I need is the value from digit 1 to the length of the value less 4. any help appreciated. Andrew RE: Newbee - substing of column - Larz60+ - Feb-17-2018 Show the code (in context) with enough to recreate what you want eliminated. RE: Newbee - substing of column - andrewjmdata - Feb-17-2018 Thanks for reply Larz. I have a dataframe/table with 10,000 records. One of the fields is called LSOA name. Example as per below: Crawley 999B Leicester NBB99 Market Harborough ZZZ1 London 111V What I want is just the place name, ie the text excluding the 4 character code (and the space) on the right. So the above would be: Crawley Leicester Market Harborough London In sql I would use select "substr(['LSOA name'],1,len(['LSOA name'])-5) as LSOA_Area from..." This means... take the field LSOA Name, take from the 1st character to the last character of the name minus 4 character from the right. So, "London 111V" loses the last 4 characters (and the spcase) and becomes "London". Thanks Andrew |