从熊猫数据框中获取字符串

时间:2021-03-27 20:13:35

标签: python pandas string dataframe

我有一个 Pandas 数据框,其中的元素是字符串但表示为“对象”:

enter image description here

我想访问字符串本身。这看起来非常简单,但我似乎无法将其转换为字符串:

enter image description here

我也按照另一篇文章的建议尝试了 vocab.iloc[0].astype(str).astype('|S') ,但这仍然打印出类似于第一张图片的内容。

编辑:我刚刚意识到数据框 titles 是词汇,条目是 NaN .... 完整的数据框看起来像 enter image description here。但是,我选择了符合数据框条目是字符串这一假设的答案。

2 个答案:

答案 0 :(得分:1)

我尝试创建一个与您的用例相当的示例片段,并从数据框中提取所需的元素。有关更多信息和解释,请参阅我的代码片段中的评论:

import io

import pandas as pd


# define some lorem ipsum sample data to work with in this snippet
data_string = """
words
Lorem
ipsum
dolor
sit
amet
consetetur
sadipscing
elitr
sed
diam
"""

# convert string to file-like StringIO and load data into a dataframe
data = io.StringIO(data_string)
df = pd.read_csv(data)

# print the dataframe we will be working with
print(df)

# printing `.info()` will show us that the dataframe consists of objects (same as in your example)
print(df.info())

# let's have a look at the element at integer based index 0 (using `.iloc[0]`)
# this returns a named Series (named `words` here) with a single element
print(df.iloc[0])

# access the series value(s) by calling `.values`
print(df.iloc[0].values)

# as this is a single element Series, we could extract the element
element, = df.iloc[0].values
print(element)

答案 1 :(得分:1)

  1. iloc[n] 用于索引 Dataframe 的一行而不是单个值,refer this page
  2. 从图片上看,数据中有 NAN 个值,您可以使用 .fillna()
  3. 替换它们
  4. 要获取 datafame 中单个值的数据类型,请尝试 df.iloc[0][0].__class__
  5. 获取单个值,例如第一个元素使用 df.iloc[0][0]
  6. 要将所有值(字符串)作为列表获取,请使用 df[<column name>].values 或在本例中仅使用 df.values 然后 .flatten() 将其从二维列表转换为一维列表

示例:

import pandas as pd #TODO
d1 = ['i', '11', '40', '42', '60' ,'50']
df = pd.DataFrame(d1)
df = df.fillna('')

print(df.iloc[0][0].__class__)

print(df.iloc[0][0])

print(df.values.flatten())

输出:

<class 'str'>

i

['i' '11' '40' '42' '60' '50']