Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Python list to pandas Series

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

like image 248
Hypothetical Ninja Avatar asked Feb 08 '14 13:02

Hypothetical Ninja


People also ask

Can you create a pandas series from a list?

Pandas series can be converted to a list using tolist() or type casting method. There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.


2 Answers

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) ) 
like image 135
Colin Bernet Avatar answered Sep 27 '22 20:09

Colin Bernet


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.

like image 27
user2314737 Avatar answered Sep 27 '22 19:09

user2314737