How to import .txt file to DataFrame as integers, not as string?

1.3k views Asked by At

I have file.txt:

1,2,3,4;5,6
7,8,2,1;
2,9;1

I need to import this data to DataFrame in to columns separated by ";", so I do:

import pandas as pd
data = pd.read_csv('file.txt', sep = ';', names = ['Col1', 'Col2'])
data = data.fillna('0')

As a result I get:

Col1       Col2
1,2,3,4    5,6
7,8,2,1    0
2,9        1

The rows have string format. But I need integers or lists of integers in every row, like:

Col1       Col2
[1,2,3,4]  [5,6]
[7,8,2,1]  [0]
[2,9]      [1]

or just numbers, not strings, no lists. How to do that?

2

There are 2 answers

0
Benjamin On BEST ANSWER

To get a list of integers in each cell you could use something like this:

for col in data.columns:
    data[col] = data[col].apply(lambda x: [int(y) for y in x.split(',')])

data.head()

    Col1            Col2
 0  [1, 2, 3, 4]    [5, 6]
 1  [7, 8, 2, 1]    [0]
 2  [2, 9]          [1]
0
a_guest On

You can either use the dtype or converters keyword of pandas.read_csv:

dtype=int
converters={'Col1': int, 'Col2': int}