Python Forum
Dividing a single column of dataframe into multiple columns based on char length
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dividing a single column of dataframe into multiple columns based on char length
#1
Question 
Hi Guys,
I am importing a dataset from a url using below function:
## Import from the url
import pandas as pd
dftemp=pd.read_csv("http://users.stat.ufl.edu/~winner/data/agedeath.dat")
print (dftemp)
print (type(dftemp))
#####
It works fine and give me a result like:
Output:
aris km 21 1 0 aris km 21 2 1 aris km 21 3 2 aris km 21 4 6183 sovr va 100 1439 6184 sovr va 101 1440 [6185 rows x 1 columns] <class 'pandas.core.frame.DataFrame'> #########
my objective:
I want to divide this single column in multiple columns -- eg.. for first row ----first column is for "aris km", 2nd for 21 and third for 1 . This should be based on the characters, like first value "aris km or sovr va is confined within 6 characters -- then 21 is occupying between 8-14 characters and last one is occupying 16 to 18 characters.

If I use split function, I will be able to break it using the space and thus 'aris" and 'km" will also be different columns while it should be same column.

How to break in this way?
Reply
#2
You posted a while ago and no responses, so I am going to stab at it. First, you need to add "header = None" to your import statement, as the first row is being read as the column names, which it isn't. Next, for your question, I would use the "one hot encoding" technique (can read about it multiple sites) using .map . Others will hopefully be able to give you better ideas, but this can give you a start.
Reply
#3
There is Series.str.extract method in Pandas which can
split data into columns. In your case it could be applied as follows:

dftemp.iloc[:,0].str.extract(r'([a-zA-Z\s]+)(\d+)(\d+)')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,971 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,563 Aug-26-2024, 04:52 PM
Last Post: Calab
  Create new column in dataframe Scott 10 3,417 Jun-30-2024, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 5,776 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Putting column name to dataframe, can't work. jonah88888 2 3,220 Jun-18-2024, 09:19 PM
Last Post: AdamHensley
  Merging rows and adding columns based on matching index pythonnewbie78 3 1,733 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  How to add columns to polars dataframe sayyedkamran 1 3,101 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 2,090 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 2,586 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 1,428 Dec-12-2022, 03:22 AM
Last Post: ill8

Forum Jump:

User Panel Messages

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