Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split column in several columns by delimiter '\' in pandas

I have a txt file which I read into pandas dataframe. The problem is that inside this file my text data recorded with delimiter ”. I need to split information in 1 column into several columns but it does not work because of this delimiter.

I found this post on stackoverflow just with one string, but I don’t understand how to apply it once I have a whole dataframe: Split string at delimiter '\' in python

After reading my txt file into df it looks something like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df

column1\tcolumn2\tcolumn3

0.1\t0.2\t0.3
0.4\t0.5\t0.6
0.7\t0.8\t0.9

Basically what I am doing now is the following:

df = pd.read_fwf('my_file.txt', skiprows = 8) #I use skip rows because there is irrelevant text
df['column1\tcolumn2\tcolumn3'] = "r'" + df['column1\tcolumn2\tcolumn3'] +"'" # i try to make it a row string as in the post suggested but it does not really work
df['column1\tcolumn2\tcolumn3'].str.split('\\',expand=True)

and what I get is just the following (just displayed like text inside a data frame)

r’0.1\t0.2\t0.3′

r’0.4\t0.5\t0.6′

r’0.7\t0.8\t0.9′

I am not very good with regular expersions and it seems a bit hard, how can I target this problem?

>Solution :

It looks like your file is tab-delimited, because of the "\t". This may work

pd.read_csv('file.txt', sep='\t', skiprows=8)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading