首页 > 解决方案 > 如何提取元组的第一个元素,它是 Python 数据框中的一列?

问题描述

我有一个如下的数据框,

id          text             senti_score
1           text A            (0.5,1)
2           text B             (0.4,0.7)
3           Nan                 None
4           text c             (0.2,0.4)

Expected output,
id          text             senti_score       new_Score
1           text A            (0.5,1)            0.5
2           text B             (0.4,0.7)         0.4
3           Nan                 None             None
4           text c             (0.2,0.4)          0.2

请注意,有些记录没有 senti_Score,它只有“None”。

有人可以帮我如何使用 python 得到这个吗?提前致谢

标签: pythonpandas

解决方案


只需使用 pandasstr访问器 +.get

df['senti_score'].str[0]

或者

df['senti_score'].str.get(0)

推荐阅读