Array to list python pandas

Series.tolist() – Convert Pandas Series to List

Pandas Series.tolist() method is used to convert a Series to a list in Python. In case you need a Series object as a return type use series() function to easily convert the list, tuple, and dictionary into a Series. In this article, we can see how to convert the pandas series to a list, and also we can see how to convert the Pandas DataFrame column to a list with several examples.

1. Quick Examples to Convert Series to list

If you are in hurry below are some quick examples of how to convert series to list.

 # Below are some quick examples. # Example 1: Convert pandas Series to List data = s = pd.Series(data) listObj = s.tolist() # Example 2: Convert the Course column of the DataFrame to a list listObj = df['Courses'].tolist() # Example 3: Convert a pandas series to a list using type casting listObj = list(s) # Example 4: Convert a DataFrame column to a list using type casting listObj = list(df['Courses']) 

2. Syntax of Pandas.Series.tolist()

Following is the syntax of the Pandas.Series.tolist() .

 # Syntax of Series.tolist() Pandas.Series.tolist() 

It returns the list of values.

Читайте также:  Relative position margin css

3. Create Series From Dictionary

pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). In pandas Series, the row labels of the Series are called the index. The Series can have only one column. A List, NumPy Array, Dict can be turned into a Series.

The following example creates a Series from a Python dictionary using pd.Series() function.

 # Create a Dict from a input import pandas as pd data = s = pd.Series(data) print (s) 
 # Output: Courses pandas Fees 20000 Duration 30days dtype: object 

4. Usage of Pandas Series tolist()

In Python, pandas is the most efficient library for providing various functions to convert one data structure to another data structure. Series.tolist() is one of the functions to convert the structure of the data. Using this function we are able to convert Series to Python list easily. Let’s take an example.

 # Create a list from Series listObj = s.tolist() print("Our list:", listObj) # Output : # Our list: ['pandas', 20000, '30days'] 

5. Convert DataFrame Column (Series) to List

We consider that the columns of a pandas DataFrame are pandas Series objects hence, we can convert the columns of DataFrame into a list using the tolist() method. First, let’s create Pandas DataFrame from dictionary using panads.DataFrame() function and then use tolist() to convert one of the column (series) to list. For example,

 # Create Dict object courses = # Create DataFrame from dict df = pd.DataFrame.from_dict(courses) print(df) 
 # Output: Courses Fee Duration 0 Spark 20000 35days 1 PySpark 20000 35days 2 Java 15000 40days 3 pandas 20000 30days 

After creating DataFrame we have to pass one of its columns which, we want to convert to a list into this function, it returns the series as a list.

 # Convert the Course column of the DataFrame to a list listObj = df['Courses'].tolist() print("Our list:", listObj) print(type(listObj)) 
 # Output: Our list: ['Spark', 'PySpark', 'Java', 'PHP'] 

6. Use Type Casting Method Convert Series to List

Type casting is the process to convert one datatype to another datatype. Using type casting we can convert a series to a list in pandas directly. For that, we need to pass the series into the list() function.

 # Convert a pandas series to a list using type casting listObj = list(s) print('Our list:', listObj) # Output : # Our list: ['pandas', 20000, '30days'] 

We can also perform type casting to convert a DataFrame column to a list. For example.

 # Convert a DataFrame column to a list using type casting listObj = list(df['Courses']) print('Our list:', listObj) # Output : # Our list: ['Spark', 'PySpark', 'Java', 'PHP'] 

Conclusion

In this article, I have explained to convert pandas Series into a Python list by using Series.tolist() method and also explained converting data columns to list and also using type casting.

References

You may also like reading:

Источник

Convert pandas.DataFrame, Series and list to each other

pandas.DataFrame , pandas.Series and Python’s built-in type list can be converted to each other.

This article describes the following contents.

  • Convert list to pandas.DataFrame , pandas.Series
    • For data-only list
    • For list containing data and labels (row/column names)
    • Convert data to list
    • Convert data and label (row/column name) to list
    • Convert labels (row/column names) to list

    Convert list to pandas.DataFrame , pandas.Series

    For data-only list

    By passing a list type object to the first argument of each constructor pandas.DataFrame() and pandas.Series() , pandas.DataFrame and pandas.Series are generated based on the list.

    An example of generating pandas.Series from a one-dimensional list is as follows. You can also specify a label with the parameter index .

    import pandas as pd l_1d = [0, 1, 2] s = pd.Series(l_1d) print(s) # 0 0 # 1 1 # 2 2 # dtype: int64 s = pd.Series(l_1d, index=['row1', 'row2', 'row3']) print(s) # row1 0 # row2 1 # row3 2 # dtype: int64 

    An example of generating pandas.DataFrame from a two-dimensional list (list of lists) is as follows. You can also specify the row name with the parameter index and the column name with the parameter columns .

    l_2d = [[0, 1, 2], [3, 4, 5]] df = pd.DataFrame(l_2d) print(df) # 0 1 2 # 0 0 1 2 # 1 3 4 5 df = pd.DataFrame(l_2d, index=['row1', 'row2'], columns=['col1', 'col2', 'col3']) print(df) # col1 col2 col3 # row1 0 1 2 # row2 3 4 5 

    After generating pandas.DataFrame and pandas.Series , you can set and change the row and column names by updating the index and columns attributes.

    For list containing data and labels (row/column names)

    Here’s how to generate pandas.Series from a list of label and value pairs.

    Break it down into a list of labels and a list of values and pass them to pandas.Series() . For details of processing using * and zip() , see the following article.

    l_1d_index = [['Alice', 0], ['Bob', 1], ['Charlie', 2]] index, value = zip(*l_1d_index) print(index) # ('Alice', 'Bob', 'Charlie') print(value) # (0, 1, 2) s_index = pd.Series(value, index=index) print(s_index) # Alice 0 # Bob 1 # Charlie 2 # dtype: int64 

    Here’s how to create a pandas.DataFrame from a list of labels and multiple values.

    The list can be decomposed as in the above example of pandas.Series , but it is easier to set the index with the set_index() method after reading the whole list.

    l_2d_index = [['Alice', 0, 0.0], ['Bob', 1, 0.1], ['Charlie', 2, 0.2]] df_index = pd.DataFrame(l_2d_index, columns=['name', 'val1', 'val2']) print(df_index) # name val1 val2 # 0 Alice 0 0.0 # 1 Bob 1 0.1 # 2 Charlie 2 0.2 df_index_set = df_index.set_index('name') print(df_index_set) # val1 val2 # name # Alice 0 0.0 # Bob 1 0.1 # Charlie 2 0.2 

    If the data type dtype is different for each column as in this example, the optimal dtype for each column is automatically selected.

    print(df_index_set.dtypes) # val1 int64 # val2 float64 # dtype: object 

    If the original list also contains column names, specify the first line as columns and the second and subsequent lines as the first argument.

    l_2d_index_columns = [['name', 'val1', 'val2'], ['Alice', 0, 0.0], ['Bob', 1, 0.1], ['Charlie', 2, 0.2]] df_index_columns = pd.DataFrame(l_2d_index_columns[1:], columns=l_2d_index_columns[0]) print(df_index_columns) # name val1 val2 # 0 Alice 0 0.0 # 1 Bob 1 0.1 # 2 Charlie 2 0.2 df_index_columns_set = df_index_columns.set_index('name') print(df_index_columns_set) # val1 val2 # name # Alice 0 0.0 # Bob 1 0.1 # Charlie 2 0.2 

    Convert pandas.DataFrame , pandas.Series to list

    Convert data to list

    Since there is no method to convert pandas.DataFrame , pandas.Series directly to list , first get the NumPy array ndarray with the values attribute, and then use tolist() method to convert to list .

    s = pd.Series([0, 1, 2]) print(s) # 0 0 # 1 1 # 2 2 # dtype: int64 l_1d = s.values.tolist() print(l_1d) # [0, 1, 2] 
    df = pd.DataFrame([[0, 1, 2], [3, 4, 5]]) print(df) # 0 1 2 # 0 0 1 2 # 1 3 4 5 l_2d = df.values.tolist() print(l_2d) # [[0, 1, 2], [3, 4, 5]] 

    The values attribute does not include labels (row/column names).

    s_index = pd.Series([0, 1, 2], index=['row1', 'row2', 'row3']) print(s_index) # row1 0 # row2 1 # row3 2 # dtype: int64 l_1d = s_index.values.tolist() print(l_1d) # [0, 1, 2] 
    df_index = pd.DataFrame([[0, 1, 2], [3, 4, 5]], index=['row1', 'row2'], columns=['col1', 'col2', 'col3']) print(df_index) # col1 col2 col3 # row1 0 1 2 # row2 3 4 5 l_2d = df_index.values.tolist() print(l_2d) # [[0, 1, 2], [3, 4, 5]] 

    Convert data and label (row/column name) to list

    If you want to keep the label as list data, reset the index with the reset_index() method.

    l_1d_index = s_index.reset_index().values.tolist() print(l_1d_index) # [['row1', 0], ['row2', 1], ['row3', 2]] 

    Since there is no method to reset columns , if you want to keep both the row name and column name of pandas.DataFrame as list data, after applying the reset_index() method, transpose it with .T , apply the reset_index() method again, and then restore it with .T .

    l_2d_index = df_index.reset_index().values.tolist() print(l_2d_index) # [['row1', 0, 1, 2], ['row2', 3, 4, 5]] l_2d_index_columns = df_index.reset_index().T.reset_index().T.values.tolist() print(l_2d_index_columns) # [['index', 'col1', 'col2', 'col3'], ['row1', 0, 1, 2], ['row2', 3, 4, 5]] 

    Convert labels (row/column names) to list

    If you want to convert only the label into list , use index attribute for pandas.Series .

    The index attribute is of the Index type ( RangeIndex type in the case of the default sequence number) and has a tolist() method.

    print(s_index) # row1 0 # row2 1 # row3 2 # dtype: int64 print(s_index.index) # Index(['row1', 'row2', 'row3'], dtype='object') print(type(s_index.index)) # print(s_index.index.tolist()) # ['row1', 'row2', 'row3'] print(type(s_index.index.tolist())) # 

    Similarly, for pandas.DataFrame , use the index attribute for row labels and the columns attribute for column labels. Both are of Index type.

    print(df_index) # col1 col2 col3 # row1 0 1 2 # row2 3 4 5 print(df_index.index) # Index(['row1', 'row2'], dtype='object') print(df_index.index.tolist()) # ['row1', 'row2'] print(df_index.columns) # Index(['col1', 'col2', 'col3'], dtype='object') print(df_index.columns.tolist()) # ['col1', 'col2', 'col3'] 

    The Index type can be used as it is in for , and the element can be obtained by specifying the position with [] . In many cases, there is no need to convert it to list .

    You can also use slices, but you cannot change elements.

    for i in s_index.index: print(i, type(i)) # row1 # row2 # row3 print(s_index.index[0]) # row1 print(s_index.index[:2]) # Index(['row1', 'row2'], dtype='object') # s_index.index[0] = 'ROW1' # TypeError: Index does not support mutable operations 

    Use rename() if you want to change the index or columns element.

    Источник

Оцените статью