python - Selecting the last element of a list inside a pandas dataframe
问题描述
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
解决方案
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
推荐阅读
- ruby-on-rails - 渲染向导或视图取决于我在 Rails 中的选择输入
- reactjs - React Hook useEffect 缺少依赖项:'props'。包括它或删除依赖数组。useEffect 中的道具没有数据
- java - 在 FOP API 中使用代理连接到 URL
- python - 正则表达式模式与 hbase 输出匹配
- java - 有没有办法在启动该应用程序时禁用/启用 javaFx 应用程序中的元素,即窗格和按钮
- sql - 是否可以将一个表中的 2 列连接到同一个表中?
- excel - 用于更改查询 xls 源提示的 vba 代码
- postgresql - 如何使用悲观锁实现 SKIP LOCKED 功能?
- python - 将不均匀的字典写入单独的 csv 文件
- google-apps-script - 如何通过电子表格创建谷歌表单