Convert Python list to pandas Series

113,903

Solution 1

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

Solution 2

To convert the list myList to a Pandas series use:

mySeries = pd.Series(myList) 

This is also one of the basic ways for creating a series from a list in 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).

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 dtype as integer:

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.

Solution 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

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

Solution 4

pd.Series(l) actually works on almost any type of list and it returns 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    sentence 1
1    sentence 2
2    sentence 3
dtype: object <class 'pandas.core.series.Series'>
Share:
113,903

Related videos on Youtube

Hypothetical Ninja
Author by

Hypothetical Ninja

Surviving.

Updated on July 09, 2022

Comments

  • Hypothetical Ninja
    Hypothetical Ninja almost 2 years

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

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

    • Hypothetical Ninja
      Hypothetical Ninja about 6 years
      @smci this is really embarrassing for me, a question from the time when I was a newbie. You have edited it already, looks good now.
    • smci
      smci about 6 years
      HypotheticalNinja: it's a perfectly good canonical question on an important basic topic.
  • Colin Bernet
    Colin Bernet about 10 years
    from your edits and comments, I understand that the list you're talking about is a list of lists. you have to make it 1D to make the Series. I edited my post to show how to do that with a generator.
  • Hypothetical Ninja
    Hypothetical Ninja about 10 years
    it was simple.. df = pd.Series(data) .. automatically converted the whole text into a dataframe object.. thanks.. you can edit your post and include this too,for others to benefit.. :)
  • Colin Bernet
    Colin Bernet about 10 years
    ok, still not sure about what a sentence is in your case, but I'm happy I could help anyway :-) - cheers