将 Python 列表转换为 Pandas 系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21646791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:20:11  来源:igfitidea点击:

Convert Python list to pandas Series

pythonlistpandasdataframeseries

提问by Hypothetical Ninja

What is the method to convert a Python list of strings to a pd.Seriesobject?

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

(pandas Series objects can be converted to list using tolist()method--but how to do the reverse conversion?)

(可以使用tolist()方法将熊猫系列对象转换为列表——但如何进行反向转换?)

采纳答案by Colin Bernet

I understand that your list is in fact a list of lists

我了解您的清单实际上是一份清单

import pandas as pd

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

回答by JustCurious

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

文档

Even if sentence_listis a list of list, this code still converts a list to Pandas Series object.

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

回答by prosti

pd.Series(l)actually works on almost any type of list and it returns Series object:

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'>

回答by user2314737

To convert the list myListto a Pandas series use:

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

mySeries = pd.Series(myList) 

This is also one of the basic ways for creating a series from a list in Pandas.

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

Example:

例子:

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

Note that Pandas will guess the data type of the elements of the list because a series doesn't admit mixed types (contrary to Python lists). In the example above the inferred datatype was object(the Python string) because it's the most general and can accommodate all other data types (see data types).

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

It's possible to specify a data type when creating a series:

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

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')

One can specify dtypeas integer:

可以指定dtype为整数:

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

But this will work only if all elements in the list can be casted to the desired data type.

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