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)
df.column1.str[-1]
? – Nonetheless