How do I convert csv string to list in pandas?

erip

I'm working with a csv file that has the following format:

"Id","Sequence"
3,"1,3,13,87,1053,28576,2141733,508147108,402135275365,1073376057490373,9700385489355970183,298434346895322960005291,31479360095907908092817694945,11474377948948020660089085281068730"
7,"1,2,1,5,5,1,11,16,7,1,23,44,30,9,1,47,112,104,48,11,1,95,272,320,200,70,13,1,191,640,912,720,340,96,15,1,383,1472,2464,2352,1400,532,126,17,1,767,3328,6400,7168,5152,2464,784,160,19,1,1535,7424"
8,"1,2,4,5,8,10,16,20,32,40,64,80,128,160,256,320,512,640,1024,1280,2048,2560,4096,5120,8192,10240,16384,20480,32768,40960,65536,81920,131072,163840,262144,327680,524288,655360,1048576,1310720,2097152"
11,"1,8,25,83,274,2275,132224,1060067,3312425,10997342,36304451,301432950,17519415551,140456757358,438889687625,1457125820233,4810267148324,39939263006825,2321287521544174,18610239435360217"

I'd like to read this into a data frame with the type of df['Id'] to be integer-like and the type of df['Sequence'] to be list-like.

I currently have the following kludgy code:

def clean(seq_string):
    return list(map(int, seq_string.split(',')))

# Read data
training_data_file = "data/train.csv"    
train = pd.read_csv(training_data_file)
train['Sequence'] = list(map(clean, train['Sequence'].values))

This appears to work, but I feel like the same could be achieved natively using pandas and numpy.

Does anyone have a recommendation?

alecxe

You can specify a converter for the Sequence column:

converters: dict, default None

Dict of functions for converting values in certain columns. Keys can either be integers or column labels

train = pd.read_csv(training_data_file, converters={'Sequence': clean})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a list into a string with spaces in Python?

How to convert a List<String> list to csv string

How do I convert this list of dictionaries to a csv file?

How do I convert a list of ascii values to a string in python?

How do I convert a string to a list of chars?

How do I convert an array list to hash separated string?

How do I convert a list of chars to a string in purescript

How to convert string back to list using Pandas

How do I convert a string to a list of Maybe Int

How do I convert Pandas DateTime Quarter object into string

How do I convert a TreeMap that is a <String, <List<String>> to String[][]?

How do i convert a dictionary with unequal list as value to csv

How do i convert python list into pandas dataframe with predefined columns

How do I convert this list of dictionaries to a table or csv file?

How do I write a List<String> into a .csv file as a single column?

How do I convert a list of numbers to a string in k?

How do i convert a String to a List<Map<String,String>>

how do I convert a list of custom type to a list of String

How do I separate each line of a .csv file into a string list>

How do I convert a list represented as a string to a list?

How do I convert Pandas object to a list in python3

How do I convert a list of integers to a string - Python

in python how do i convert a list of strings to pandas data frame

How do I convert List<String?> to List<String> in .NET 6 / C# 10?

How do I convert '$ -' from a string to a float using pandas?

How can I convert list of string to pandas DataFrame in Python

How can I convert a List<String> to String []?

How do I deterministically convert Pandas string columns into specific numbers?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive