Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python列表转换为熊猫系列_Python_List_Pandas_Dataframe_Series - Fatal编程技术网

将Python列表转换为熊猫系列

将Python列表转换为熊猫系列,python,list,pandas,dataframe,series,Python,List,Pandas,Dataframe,Series,将Python字符串列表转换为pd.Series对象的方法是什么 (熊猫系列对象可以使用tolist()方法转换为列表--但是如何进行反向转换?我知道您的列表实际上是列表的列表 import pandas as pd thelist = [ ['sentence 1'], ['sentence 2'], ['sentence 3'] ] df = pd.Series( (v[0] for v in thelist) ) 即使语句\u list是一个列表列表,此代码仍然会将列表转换为Pand

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


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

我知道您的列表实际上是列表的列表

import pandas as pd

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

即使
语句\u list
是一个列表列表,此代码仍然会将列表转换为Pandas Series对象。

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

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第1句
1第2句
第2句第3句
数据类型:对象

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

mySeries = pd.Series(myList) 
这也是从熊猫列表中创建系列的基本方法之一

例如:

myList = ['string1', 'string2', 'string3']                                                                                                                
mySeries = pd.Series(myList)                                                                                                                             
mySeries                                                                                                                                                 
# Out: 
# 0    string1
# 1    string2
# 2    string3
# dtype: object
请注意,Pandas将猜测列表元素的数据类型,因为一个系列不允许混合类型(与Python列表相反)。在上面的示例中,推断的数据类型是
object
(Python
string
),因为它是最通用的,可以容纳所有其他数据类型(请参阅)

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

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

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

从您的编辑和评论中,我了解到您所说的列表是一个列表列表。你必须把它做成1D来制作这个系列。我编辑了我的帖子,展示了如何用发电机实现这一点。。df=pd.系列(数据)。。自动将整个文本转换为dataframe对象。。谢谢您可以编辑您的帖子并将其包括在内,以使其他人受益:)好的,我仍然不确定你的情况下的句子是什么,但我很高兴我能帮上忙:-)cheers@smci这对我来说真的很尴尬,这是我还是新手时的问题。假设Inja:这是一个非常好的关于一个重要基本主题的规范性问题。
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')
myList= ['1', 2.2, '3']
mySeries = pd.Series(myList, dtype='int')  
mySeries.dtype                                                                                                                                 
# Out:
# dtype('int64')