richie
richie

Reputation: 18638

Concatenate columns and create a list

I would like to concatenate a couple of columns that result in either a list of strings or a list of ints.

I've done this so far;

data1 = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})

data1['D'] = [' '.join(str(row)) for row in data1[data1.columns[0:3]].values] # add a new column by concatenating the existing columns

   A  B  C              D
0  1  4  7  [ 1   4   7 ]
1  2  5  8  [ 2   5   8 ]
2  3  6  9  [ 3   6   9 ]

How do I get the string '[1 4 7]' converted as a list of strings like ['1','4','7'] or a list of ints like [1,4,7] ?

Upvotes: 0

Views: 488

Answers (3)

Xiphias
Xiphias

Reputation: 4716

You do not have to concatenate them to strings if it is not necessary.

import pandas as pd
data1 = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
longs = data1.iloc[0].tolist()

iloc[0] selects the first item of the DataFrame, and tolist() converts it to a list. You get a list of numbers of type long.

Then use this to convert the lists values to int:

ints = [int(x) for x in longs]

Long story short:

ints = [int(x) for x in data1.iloc[0].tolist()]

Upvotes: 2

Alvaro Fuentes
Alvaro Fuentes

Reputation: 17455

Just don't use join:

data1['E'] = [row for row in data1[data1.columns[0:3]].values]

Upvotes: 2

amsh
amsh

Reputation: 210

Using a regular expression: (can be easily modified to extract negative floats...)

>>> import re
>>> re.findall('(\d+)', '[1 4 7]')
['1', '4', '7']

Upvotes: 2

Related Questions