I have pandas dataframe df in below format
Title
ABCABCABC
ABCDABCDABCD
ABCDEABCDEABCDE
ABEABEABE
I want to split the Title
column into 3 equal parts.
Expected output:
Title1 Title2 Title3
ABC ABC ABC
ABCD ABCD ABCD
ABCDE ABCDE ABCDE
ABE ABE ABE
please help me know how to do it.
I tried to get length, but not sure how to split based on length value.
df['len'] = df.Title.str.len()
Using the builtin textwrap
module.
import textwrap
pd.DataFrame(
[textwrap.wrap(el, len(el)//3) for el in df['Title']]
).add_prefix('Title')
Title0 Title1 Title2
0 ABC ABC ABC
1 ABCD ABCD ABCD
2 ABCDE ABCDE ABCDE
3 ABE ABE ABE
Here is one way:
pd.DataFrame([np.split(np.array(list(i)),3) for i in df.Title]).applymap(''.join)
0 1 2
0 ABC ABC ABC
1 ABCD ABCD ABCD
2 ABCDE ABCDE ABCDE
3 ABE ABE ABE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.