Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding column to pandas DataFrame containing list of other columns' values

I have a DataFrame to which I need to add a column. The column needs to be a list of two values:

Current table:

    lat  long  other_value
0   50   50    x
1   60   50    y
2   70   50    z
3   80   50    a

Needed table:

    lat  long  other_value  new_column
0   50   50    x            [50, 50]
1   60   50    y            [60, 50]
2   70   50    z            [70, 50]
3   80   50    a            [80, 50]

I know this is super simple, but the documentation doesn't seem to cover this (at least not apparently).

like image 347
Eric Miller Avatar asked Feb 23 '15 19:02

Eric Miller


2 Answers

One way is to use tolist():

>>> df['new_column'] = df[['lat', 'long']].values.tolist()
>>> df
   lat  long other_value new_column
0   50    50           x   [50, 50]
1   60    50           y   [60, 50]
2   70    50           z   [70, 50]
3   80    50           a   [80, 50]

In general though, I'd be very wary of using lists in DataFrames since they're more difficult to manipulate in columns and you don't get many of the performance benefits that come with integers/floats.

like image 200
Alex Riley Avatar answered Oct 02 '22 16:10

Alex Riley


you could use zip

df['new_column'] = list(zip(df.lat, df.long))
like image 20
Haleemur Ali Avatar answered Oct 02 '22 14:10

Haleemur Ali