18

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).

2 Answers 2

26

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I've wonder about lists in dataframes. Any info on why you can't check if a location in a df is an empty list ?
I updated my problem to include an additional column. That's why .values won't work. That is, unless I can say .values and specify the columns to use. Can I do that you think?
@LiamFoley: I'm not sure to be honest. I expect that the Pandas developers never really had lists (or other Python data structures) in mind for df values and so they're not really supported. Perhaps that will change for future releases. In the mean time, some of the str methods work alright with lists, e.g. df['new_column'].str.len() == 0 will check if a list is empty.
Also, I believe lists are converted into numpy array objects if you need to access the column individually. (not 100% sure though)
2

you could use zip

df['new_column'] = list(zip(df.lat, df.long))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.