Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Split Column Text by Number of Characters
#1
Hello all,

Brand new to Python! I've tried search keywords for this solution but came up with unrelated examples with commas / delimiters. Any assistance would be greatly appreciated.

Loading from csv into a df,

Data set column = "Month of Year" - values (201801,201802,201803)
I would like to split up this column into a Month and a Year column ( Month = 01, Year = 2018 ).

In excel, this would be a text to column function, any idea on what the equivalent would be in python?

Thank you
Quote
#2
I suspect that Excel 'text-to-columns' function doesn't split value to year and month without delimiter as well.

If somebody is brand new to Python it's not good to start with Pandas right away. Learning Python basics will serve you well in future.

There appears to be no delimiter to split value. However, it seems that there is pattern: first four represent year, next two represent month. If values coming from csv then by default they are strings and slicing can retrieve needed data:

>>> values = '201801,201802,201803'
>>> [(el[4:], el[:4]) for el in values.split(',')]
[('01', '2018'), ('02', '2018'), ('03', '2018')] 
snippsat likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
To take @perfringo code a step further and make it datetime object.
>>> values = '201801,201802,201803'
>>> d = [(el[4:], el[:4]) for el in values.split(',')]
>>> d
[('01', '2018'), ('02', '2018'), ('03', '2018')]
>>> date_str = ['-'.join(i) for i in d]
>>> date_str
['01-2018', '02-2018', '03-2018']
>>> date_str[0]
'01-2018'
So a this point has a delimiter(-) and now can use strftime().
>>> from datetime import datetime, timedelta
>>> 
>>> d = date_str[0]
>>> d
'01-2018'
>>> my_date = datetime.strptime(d, '%m-%Y')
>>> my_date
datetime.datetime(2018, 1, 1, 0, 0)

>>> # So at this point is a datetime object
>>> type(my_date)
<class 'datetime.datetime'>
>>> my_date.year
2018
>>> my_date.month
1

>>> # 5 days from my_date
>>> my_date + timedelta(days=5)
datetime.datetime(2018, 1, 6, 0, 0)

# With print() don't show "repr" object info
>>> print(my_date + timedelta(days=5))
2018-01-06 00:00:00
Pandas do of course have own Time Series / Date functionality which has a lot functionally.
Quote
#4
Just because it can be done: based on snippsat code one can develop following dense and 'nice' code:

>>> from datetime import datetime
>>> values = '201801,201802,201803'
>>> [datetime.strptime(f'{el[4:]}-{el[:4]}', '%m-%Y') for el in values.split(',')]
[datetime.datetime(2018, 1, 1, 0, 0),
 datetime.datetime(2018, 2, 1, 0, 0),
 datetime.datetime(2018, 3, 1, 0, 0)]
>>> [(date.month, date.year) for date in [datetime.strptime(f'{el[4:]}-{el[:4]}', '%m-%Y') for el in values.split(',')]]
[(1, 2018), (2, 2018), (3, 2018)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Check for a special characters in a column and flag it ayomayam 0 107 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  Split a long string into other strings with no delimiters/characters krewlaz 4 225 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Split csv file based on column value soli004 4 793 Oct-22-2019, 05:53 AM
Last Post: soli004
  Trying to make column based file from text file scor1pion 7 517 Jul-16-2019, 02:43 PM
Last Post: scor1pion
  get the number in the line in text file lateublegende 2 542 Jan-29-2019, 06:03 PM
Last Post: lateublegende
  [split] Bad magic number. What is it ? Douglas 2 821 Oct-22-2018, 10:38 AM
Last Post: Douglas
  get number of unread emails + email text Pedroski55 3 693 Oct-04-2018, 12:33 PM
Last Post: Larz60+
  Openpyxl -Coming up with a single column from a multi column data set Givan007 1 694 Sep-07-2018, 04:29 AM
Last Post: Prabakaran141
  [split] Offline audio to text (Speech Recognition) Nishant260190 0 1,755 Sep-02-2018, 12:33 PM
Last Post: Nishant260190
  Counting number of characters in a string Drone4four 1 825 Aug-16-2018, 02:33 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)