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

select specific rows in text file to read into two separate columns in a pandas dataframe

I have a text file like the following

2022-mary
['apple', 'oranges', 'pineapple']
2022-cindy
['mango', 'banana', 'berry']
2022-andrew
['coconut', 'cabbage']

I would like to produce a pandas dataframe with 2 columns from the text file above that looks like this

    user            fruits
0   2022-mary      ['apple', 'oranges', 'pineapple']
1   2022-cindy     ['mango', 'banana', 'berry']
2   2022-andrew    ['coconut', 'cabbage']

I wonder if there is an efficient way to accomplish this with pd.read_csv() functions.

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

>Solution :

Create one column DataFrame and then selecting even and odd values:

df = pd.read_csv(file, sep="|", names=['data'])

df = pd.DataFrame({'user': df.data.iloc[::2].to_numpy(),
                   'fruits': df.data.iloc[1::2].to_numpy()})

Or create list:

with open(file) as f:
    lines = f.read().splitlines()

df = pd.DataFrame({'user': lines[::2], 'fruits': lines[1::2]})
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