我有一个 Pandas 数据框,其中的元素是字符串但表示为“对象”:
我想访问字符串本身。这看起来非常简单,但我似乎无法将其转换为字符串:
我也按照另一篇文章的建议尝试了 vocab.iloc[0].astype(str)
和 .astype('|S')
,但这仍然打印出类似于第一张图片的内容。
编辑:我刚刚意识到数据框 titles 是词汇,条目是 NaN .... 完整的数据框看起来像
。但是,我选择了符合数据框条目是字符串这一假设的答案。
答案 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)
iloc[n]
用于索引 Dataframe 的一行而不是单个值,refer this pageNAN
个值,您可以使用 .fillna()
df.iloc[0][0].__class__
df.iloc[0][0]
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']