Reputation: 465
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
Upvotes: 2
Views: 3049
Reputation: 142631
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)
Upvotes: 6
Reputation: 14216
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
Upvotes: 0
Reputation: 135
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])
Upvotes: 1
Reputation: 195408
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
Upvotes: 4