Home > Software design >  Pandas, accessing every nth element in nested array
Pandas, accessing every nth element in nested array

Time:11-17

I have a dataframe of many rows and 4 columns. Each column contains an array of 100 values.

My intuitive way of doing this is the same way I would do it with multi-dimensional numpy arrays.

For example, I want the first element of every array in column1. So I say

df["column1"][:][0]

To me this makes sense: first select the column, then take every array, then take the first element of every array.

However, it just doesn't work at all. Instead, it simply spits out the entire array from column1, row 1.

But - and this is the most frustrating thing - if I say:

df["column1"][1][0]

It gives me EXACTLY what I expect based on my expected logic, as in, I get the first element in the array in the second row in column1.

How can I get every nth element in every array in column1?

CodePudding user response:

The reason that df["column1"][:][0] isn't doing what you expect is that df["column1"][:] returns a Series. With a Series, using bracket indexing returns the item of the series at that index.

If you want to a series where each item in the series is the item in the corresponding array at that index, the correct solution - whether it seems intuitive or not - is to use .str[...] on the Series.

Instead of

df["column1"][:][0]

use this:

df["column1"].str[0]

It might seem like .str should only be used for actual str values, but a neat trick is that works for lists too.

CodePudding user response:

Here are some ways to do this:

[item[0] for item in df['column1']]  # will result in a list

or

df['column1'].apply(lambda item: item[0])  # will result in a series

Not sure if you're looking for a way that's similar to slicing, but AFAIU pandas sees the lists in your table are just arbitrary objects, not something pandas provides a sugar for.

Of course, you can do other fancy things by creating a data frame out of your column:

pd.DataFrame(df['column1'].tolist())

And then do whatever you want with it.

  • Related