【问题标题】:Get first element of Pandas Series of string获取 Pandas 系列字符串的第一个元素
【发布时间】:2013-06-12 02:12:53
【问题描述】:

我想我有一个相对简单的问题,但无法找到合适的答案来解决编码问题。

我有一个 pandas 字符串列: df1['tweet'].head(1) 0 besides food, Name: tweet

我需要提取文本并将其推送到 Python str 对象中,格式如下:

test_messages = ["line1", "line2", "etc"]

目标是对推文的测试集进行分类,因此相信输入:X_test = tfidf.transform(test_messages) 是一个 str 对象。

【问题讨论】:

标签: python string pandas series


【解决方案1】:

使用list将Series(列)转换为python列表:

list(df1["tweet"])

【讨论】:

    【解决方案2】:
    1. 获取Serieshead(),然后访问第一个值:

      df1['tweet'].head(1).item()

    2. 或者:使用 Series tolist() 方法,然后对第 0 个元素进行切片:

      df.height.tolist() [94, 170] df.height.tolist()[0] 94

    (注意 Python 索引是从 0 开始的,但 head() 是从 1 开始的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2019-05-11
      • 2017-02-13
      • 2022-11-24
      • 1970-01-01
      • 2015-01-17
      相关资源
      最近更新 更多