> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Convert Pandas Column or Row to List
- URL: https://datascientyst.com/how-to-convert-pandas-column-or-row-to-list/
- Published: 2023-03-17T21:21:25.000Z
- Updated: 2023-03-17T21:21:25.000Z
- Author: John D K
- Tags: Column

To convert a DataFrame column or row to a list in Pandas, we can use the Series method `tolist()`. Here's how to do it:

```python
df['A'].tolist()
df.B.tolist()

```

Image below shows some of the solutions described in this article:  
![](https://datascientyst.com/content/images/2023/03/convert-pandas-column-or-row-to-list.webp)

## Setup

We will use the following DataFrame to convert rows and columns to list:

```python
```python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

```

data:

|   | A | B |
| - | - | - |
| 0 | 1 | 4 |
| 1 | 2 | 5 |
| 2 | 3 | 6 |

## Convert column to list

To convert a column to a list, we can access the column using either

- the bracket notation `([])`
- or the dot notation `(.)`

and then call the `tolist()` method on the resulting pandas Series object:

```python
df['A'].tolist()
df.B.tolist()

```

result:

```
[1, 2, 3]
[4, 5, 6]

```

## Convert row to list

To get a list from a row in a Pandas DataFrame, we can use the `iloc` indexer to access the row by its index. Then call the `tolist()` method on the resulting pandas Series object:

```python
row_list = df.iloc[0].tolist()

```

result:

```
[1, 4]

```

## Convert column with list values to row

We can convert columns which have list values to rows by using method `.explode()`. The example below shows how to convert column B which has list values. We will use new DataFrame:

```python
df = pd.DataFrame({'A':['a','b'],
           	'B':[['1', '2'],['3', '4', '5']]})

```

with data:

|   | A | B           |
| - | - | ----------- |
| 0 | a | \[1, 2\]    |
| 1 | b | \[3, 4, 5\] |

To rows by:

```python
df.explode('B')

```

|   | A | B |
| - | - | - |
| 0 | a | 1 |
| 0 | a | 2 |
| 1 | b | 3 |
| 1 | b | 4 |
| 1 | b | 5 |

## Column from lists (string) to lists

Finally let's check how to convert column which contains list values stored as strings to list:

```python
import pandas as pd

data_dict = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
         	'two': pd.Series(['[1, 2]', '[3, 4]', '[5, 6]'], index=['a', 'b', 'c'])}

df = pd.DataFrame(data_dict)

```

data looks like:

|   | one | two      |
| - | --- | -------- |
| a | 1   | \[1, 2\] |
| b | 2   | \[3, 4\] |
| c | 3   | \[5, 6\] |

We can extract list values by using list comprehensions:

```python
[x.strip('[]').split(',') for x in df['two']]

```

which result into new list of lists:

```
[['1', ' 2'], ['3', ' 4'], ['5', ' 6']]

```

If we like to keep the list values in the column we can use `ast` module:

```python
import ast
df.two.apply(ast.literal_eval)

```

Which we result into:

```
a	[1, 2]
b	[3, 4]
c	[5, 6]
Name: two, dtype: object

```