简体   繁体   中英

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

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?

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

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

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]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM