【问题标题】:Convert Python list to pandas Series将 Python 列表转换为熊猫系列
【发布时间】:2014-03-05 23:44:02
【问题描述】:

将 Python 字符串列表转换为 pd.Series 对象的方法是什么?

(pandas系列对象可以使用tolist()方法转换为list--但是如何进行反向转换呢?)

【问题讨论】:

  • @smci 这对我来说真的很尴尬,这是我还是新手时的一个问题。您已经对其进行了编辑,现在看起来不错。
  • HypotheticalNinja:这是一个关于重要基本主题的非常好的规范问题。

标签: python list pandas dataframe series


【解决方案1】:

我了解您的列表实际上是列表列表

import pandas as pd

thelist = [ ['sentence 1'], ['sentence 2'], ['sentence 3'] ]
df = pd.Series( (v[0] for v in thelist) )

【讨论】:

  • 从您的编辑和 cmets 中,我了解到您所说的列表是列表列表。您必须将其制作为 1D 才能制作系列。我编辑了我的帖子以展示如何使用生成器来做到这一点。
  • 这很简单.. df = pd.Series(data) .. 自动将整个文本转换为数据框对象.. 谢谢.. 您也可以编辑您的帖子并将其包含在内,以供其他人使用受益.. :)
  • 好的,仍然不确定你的情况是什么,但我很高兴我能提供帮助:-) - 干杯
【解决方案2】:

要将列表 myList 转换为 Pandas 系列,请使用:

mySeries = pd.Series(myList) 

这也是在 Pandas 中从列表创建系列的基本方法之一。

例子:

myList = ['string1', 'string2', 'string3']                                                                                                                
mySeries = pd.Series(myList)                                                                                                                             
mySeries                                                                                                                                                 
# Out: 
# 0    string1
# 1    string2
# 2    string3
# dtype: object

请注意,Pandas 会猜测列表元素的数据类型,因为系列不允许混合类型(与 Python 列表相反)。在上面的示例中,推断的数据类型是 object(Python string),因为它是最通用的并且可以容纳所有其他数据类型(请参阅 data types)。

创建系列时可以指定数据类型:

myList= [1, 2, 3] 

# inferred data type is integer
pd.Series(myList).dtype                                                                                                                        
# Out:
# dtype('int64')

myList= ['1', 2, 3]                                                                                                                                     

# data type is object  
pd.Series(myList).dtype                                                                                                                                                                                                                                                                
# Out: 
# dtype('O')

可以将dtype指定为整数:

myList= ['1', 2.2, '3']
mySeries = pd.Series(myList, dtype='int')  
mySeries.dtype                                                                                                                                 
# Out:
# dtype('int64')

但这只有在列表中的所有元素都可以转换为所需的数据类型时才有效。

【讨论】:

    【解决方案3】:
    import pandas as pd
    sentence_list = ['sentence 1', 'sentence 2', 'sentence 3', 'sentence 4']
    print("List of Sentences: \n", sentence_list)
    sentence_series = pd.Series(sentence_list)
    print("Series of Sentences: \n", sentence_series)
    

    Documentation

    即使sentence_list 是列表的列表,此代码仍会将列表转换为 Pandas Series 对象。

    【讨论】:

      【解决方案4】:

      pd.Series(l) 实际上几乎适用于任何类型的列表,它返回 Series 对象:

      import pandas as pd
      l = [ ['sentence 1'], ['sentence 2'], ['sentence 3'] ] #works
      l = ['sentence 1', 'sentence 2', 'sentence 3'] #works
      l = numpy.array(['sentance 1', 'sentance2', 'sentance3'], dtype='object') #works
      
      print(l, type(l))
      ds = pd.Series(l)
      print(ds, type(ds))
      

      0    sentence 1
      1    sentence 2
      2    sentence 3
      dtype: object <class 'pandas.core.series.Series'>
      

      【讨论】:

        猜你喜欢
        • 2019-10-12
        • 2021-04-17
        • 1970-01-01
        • 2014-11-23
        • 2017-04-08
        相关资源
        最近更新 更多