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

Read csv with always the 1st space as the separator

I want to read a file in pandas data frame, which looks like:

    label1 text more text
    label2 text some more text

into a dataframe, which looks like:

    label              text
    label1             text more text
    label2             text some more text

My text column is without the quotes and I want to specify the 1st space as the separator and ignore all the later spaces in text field

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

I tried with the regular expression, which matches 1st space as well as the whole string before the 1st space with https://regex101.com:

       query_data = pd.read_csv('data.txt', sep='^.*?(?=\ )', engine='python',\
               names = ['label', 'text'])

But, this gives all the labels as NaNs. Then, I realized it is matching the full string before space. How do I just specify the 1st space as the delimiter?

>Solution :

You can just read the csv and split it later on

df = df.col.str.split(' ', n =1, expand=True)
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