Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbee - substing of column
#1
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
Reply
#2
Show the code (in context) with enough to recreate what you want eliminated.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file handling Newbee question middlecope 2 745 Jan-18-2023, 03:09 PM
Last Post: middlecope
  Newbee Question chas777 4 2,204 Apr-01-2020, 06:52 PM
Last Post: chas777
  Useless Newbee craigpusey 2 56,742 Mar-04-2020, 02:13 PM
Last Post: craigpusey

Forum Jump:

User Panel Messages

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