简体   繁体   English

Python Pandas Dataframe构造函数将列表转换为字符串

[英]Python Pandas Dataframe Constructor Converts List to String

is there a simple solution to solve the following problem: 有没有简单的解决方案来解决以下问题:

I'm using Pandas dataframe 我正在使用熊猫数据框

dataframe = pandas.DataFrame(data, index=[categorieid], columns=['title', 'categorieid'])

where categorieid is a list of integer values (eg [1,2,3,1]) and title a list of strings ['a','b,'c','d']. 其中categorieid是整数值(例如[1,2,3,1])的列表,标题是字符串['a','b,'c','d']的列表。

And then i'm trying to access the title at a specific position with 然后我尝试使用以下方式在特定位置访问标题

dataframe.ix[i]['title'].values.tolist()

The problem is that i get an exception if only one title exists for a given index because then pandas saves my title as a string and not as a list. 问题是,如果给定索引仅存在一个标题,我会遇到异常,因为熊猫会将我的标题另存为字符串而不是列表。

Is there a solution to tell the dataframe constructor always to create a list() at each index even if there is only one item contained? 有没有一种解决方案可以告诉数据框构造函数始终在每个索引处创建一个list(),即使其中仅包含一项?

Thank you for any help 感谢您的任何帮助

Edit: My printed dataframe looks like this 编辑:我打印的数据框看起来像这样

        categorieid title
0            0     a
0            0     c
1            1     b
1            1     d
0            0     e
2            2     f

Calling my values.tolist() results in for _title in dataframe.ix[i]['title'].values.tolist(): AttributeError: 'unicode' object has no attribute 'values' 调用我的values.tolist()会导致dataframe.ix [i] ['title']。values.tolist()中的_title:AttributeError:'unicode'对象没有属性'values'

I think you're making it more difficult than it has to be by the way you're building the DataFrame. 我认为您正在通过构建DataFrame的方式使其变得更加困难。 Also, accessing the 'values' attribute is not needed. 另外,不需要访问“值”属性。

Since you only have one dimension, you're probably better off using a Series. 由于只有一个维度,因此最好使用系列。 Then you can select the entries using the index and convert to a list. 然后,您可以使用索引选择条目并转换为列表。

In [12]: s = pd.Series(list('acbdef'), index=[0, 0, 1, 1, 0, 2], name='title')

In [13]: s
Out[13]: 
0    a
0    c
1    b
1    d
0    e
2    f
Name: title, dtype: object

In [14]: s[1].tolist()
Out[14]: ['b', 'd']

If you really need a DataFrame for some reason not mentioned, it will work similarly: 如果由于某些原因您确实需要一个DataFrame,那么它会以类似的方式工作:

In [15]: df = pd.DataFrame(s)

In [16]: df
Out[16]: 
  title
0     a
0     c
1     b
1     d
0     e
2     f

In [17]: df['title'][1].tolist()
Out[17]: ['b', 'd']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM