12/1/2021

Pandas To List

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
    • Fors 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.

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

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

      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.

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

      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.

      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.

      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.

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

      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 can't change elements.

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

Pandas To List Of Dictionaries

Pandas To ListListList
  1. Pandas library in Python easily let you find the unique values. In this tutorial, we will see examples of getting unique values of a column using two Pandas functions. We will first use Pandas unique function to get unique values of a column and then use Pandas dropduplicates function to get unique values of a column.
  2. So if we need to convert a column to a list, we can use the tolist method in the Series. Tolist converts the Series of pandas data-frame to a list. In the code below, df 'DOB' returns the Series, or the column, with the name as DOB from the DataFrame. The tolist method converts the Series to a list.

Pandas Dataframe To List

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.