Selecting the last element of a list inside a pandas dataframe
Asked Answered
E

4

2

I have a pandas dataframe with a column containing of list values with example data as:

datetime.              column1
2021-04-10 00:03 00.   [20.0, 21.6, 30.7]
2021-04-10 00:06 00.   [10.0, 20.6, 20.7]
2021-04-10 00:09 00.   [20.0, 21.5, 10.7]

I would like to select the last element of the column1 with the expected output as

datetime.              column1
2021-04-10 00:03 00.   30.7
2021-04-10 00:06 00.   20.7
2021-04-10 00:09 00.   10.7
Edgerton answered 21/4, 2021 at 18:49 Comment(2)
You do this the same way as you access the last element of any list: access the list, then access the final element of that expression. Where did you get stuck?Itu
df.column1.str[-1] ?Nonetheless
B
4
df.column1 = df.column1.apply(lambda x: x[-1])    
print(df)

Prints:

              datetime.  column1
0  2021-04-10 00:03 00.     30.7
1  2021-04-10 00:06 00.     20.7
2  2021-04-10 00:09 00.     10.7
Balderdash answered 21/4, 2021 at 18:52 Comment(0)
N
4

Maybe it looks strange but you can use .str to get element from list

 df.column1 = df.column1.str[-1]

You can also use it when you have dictionary

 df.other = df.other.str[key]

Minimal working code

import pandas as pd

df = pd.DataFrame({
    'datetime.': [
        '2021-04-10 00:03 00.', 
        '2021-04-10 00:06 00.', 
        '2021-04-10 00:09 00.'
    ],
    'column1':  [
        [20.0, 21.6, 30.7], 
        [10.0, 20.6, 20.7], 
        [20.0, 21.5, 10.7]
    ],
    'other':  [
        {'a': 20.0, 'b': 21.6, 'c': 30.7}, 
        {'a': 10.0, 'b': 20.6, 'c': 20.7}, 
        {'a': 20.0, 'b': 21.5, 'c': 10.7}
    ],
})    

print(df)

df.column1 = df.column1.str[-1]
df.other = df.other.str['c']

print(df)

Result:

              datetime.             column1                              other
0  2021-04-10 00:03 00.  [20.0, 21.6, 30.7]  {'a': 20.0, 'b': 21.6, 'c': 30.7}
1  2021-04-10 00:06 00.  [10.0, 20.6, 20.7]  {'a': 10.0, 'b': 20.6, 'c': 20.7}
2  2021-04-10 00:09 00.  [20.0, 21.5, 10.7]  {'a': 20.0, 'b': 21.5, 'c': 10.7}


              datetime.  column1  other
0  2021-04-10 00:03 00.     30.7   30.7
1  2021-04-10 00:06 00.     20.7   20.7
2  2021-04-10 00:09 00.     10.7   10.7

To do the same with many columns at once you would need also .apply()

df[['column1', 'column2']] = df[['column1', 'column2']].apply(lambda column: column.str[-1]) # axis=0

or in rows

df[['column1', 'column2']] = df[['column1', 'column2']].apply(lambda row: row.str[-1], axis=1)

BTW:

If you would like to convert all elements to columns then you can use .apply(pd.Series)

df[ ["1", "2", "3"] ] = df.column1.apply(pd.Series)
df[ ["a", "b", "c"] ] = df.other.apply(pd.Series)
Nonetheless answered 21/4, 2021 at 19:24 Comment(0)
F
1

There are no builtin methods for dealing with lists in Pandas, but you can use apply().

df.column1 = df.column1.apply(lambda x: x[-1]) 
Frail answered 21/4, 2021 at 18:55 Comment(0)
I
0

An approach without using apply, which is the same as iterating over the DataFrame line by line, is to put the column in to a new DataFrame using the standard constructor.

df.assign(new_column1=pd.DataFrame(df.column1.tolist()).iloc[:, -1])

              column1  new_column1
0  [20.0, 21.6, 30.7]         30.7
1  [10.0, 20.6, 20.7]         20.7
2  [20.0, 21.5, 10.7]         10.7
Indeciduous answered 21/4, 2021 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.