> ## 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 Split Column into Multiple Columns in Pandas
- URL: https://datascientyst.com/split-pandas-list-column-into-multiple-columns/
- Published: 2021-10-28T13:23:38.000Z
- Updated: 2022-02-19T07:44:44.000Z
- Author: John D K
- Tags: split()

## 1\. Overview

Here are two approaches to **split a column into multiple columns in Pandas:**

- list column
- string column separated by a delimiter.

Below we can find both examples:

(1) **Split column (list values) into multiple columns**

```python
pd.DataFrame(df["langs"].to_list(), columns=['prim_lang', 'sec_lang'])

```

(2) **Split column by delimiter in Pandas**

```python
pd.DataFrame(df["skills"].str.split(',').fillna('[]').tolist())

```

In **Pandas to split column we can use method `.str.split(',')`** \- which takes a delimiter as a parameter.

Next we will see how to apply both ways into practical examples.

## 2\. Split List Column into Multiple Columns

For the first example we will create a simple DataFrame with 1 column which stores a list of two languages. We are going to generate 10 random lists of subset of languages:

```python
import random

langs = ['Python', 'Java' , 'JS', 'C', 'C+']

df = pd.DataFrame({"langs": [ [random.choice(langs) for i in range(0,2)] for _ in range(10)]})

```

Our DataFrame looks like this:

| langs          |
| -------------- |
| \[C+, C+\]     |
| \[Python, JS\] |
| \[C+, C+\]     |
| \[Java, Java\] |
| \[Java, C\]    |

In order to **split this single column(which contain list values) into two columns** we will use the next syntax:

```python
pd.DataFrame(df["langs"].to_list(), columns=['prim_lang', 'sec_lang'])

```

The result of the split is:

| prim\_lang | sec\_lang |
| ---------- | --------- |
| C+         | C+        |
| Python     | JS        |
| C+         | C+        |
| Java       | Java      |
| Java       | C         |

How does it work? The method `df["langs"].to_list()` is converting the initial column into list of lists:

```
[['C+', 'C+'],
 ['Python', 'JS'],
 ['C+', 'C+'],
 ['Java', 'Java'],
 ['Java', 'C'],
 ['Java', 'C+'],
 ['Python', 'C'],
 ['Python', 'C+'],
 ['C', 'Java'],
 ['C+', 'JS']]

```

Note: This method will work only if the stored values are lists. If you have string values separated by columns check Example 2.

![](https://datascientyst.com/content/images/2021/10/split-pandas-list-column-into-multiple-columns.png)

## 3\. Split Column by delimiter in Pandas

Now let's say that instead of storing lists like: `['C+', 'C+']` you have only the values separated by delimiter. In this case is a comma like `'C+', 'C+'`.

Lets have data like the one below:

| skills                                                                                                               | internship                                       | location       |
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ | -------------- |
| Software Testing                                                                                                     | Software Testing                                 | Noida          |
| Java, SQL, Unix, Oracle, MS SQL Server, Hibernate (Java), Shell Scripting, Spring MVC, REST API                      | Technical Operations - Networking And Monitoring | Noida          |
| English Proficiency (Spoken), English Proficiency (Written), Hindi Proficiency (Spoken), Hindi Proficiency (Written) | Software Project Management                      | Work From Home |
| HTML, CSS, Flask, Python, Django                                                                                     | Web Development                                  | Work From Home |
| HTML, CSS, JavaScript, ReactJS, Redux                                                                                | Front End Development                            | Work From Home |

And we would like to **split the column skills by delimiter into multiple columns**. This time the number of elements is not fixed!

We can use Pandas string method `.str.split(',')` in order to split the values into lists of lists. If you have missing data you need to ensure that you default it by empty list by `.fillna('[]')`:

```python
pd.DataFrame(df["skills"].str.split(',').fillna('[]').tolist())

```

This will create DataFrame like:

| 0                            | 1                             | 2                          | 44   | 45   |
| ---------------------------- | ----------------------------- | -------------------------- | ---- | ---- |
| Software Testing             | None                          | None                       | None | None |
| Java                         | SQL                           | Unix                       | None | None |
| English Proficiency (Spoken) | English Proficiency (Written) | Hindi Proficiency (Spoken) | None | None |
| HTML                         | CSS                           | Flask                      | None | None |
| HTML                         | CSS                           | JavaScript                 | None | None |

As you can see the result DataFrame has 45 columns. Which means that one of the rows has 45 values separated by comma.

In order to find which row(s) have most values we can use syntax like - test the last column for all non null elements:

```python
pd.DataFrame(df["skills"].str.split(',').fillna('[]').tolist())[45].dropna()

```

And we get as output:

```
1242     Gitlab

```

## 4\. Conclusion and Resources

In this guide we saw how to split columns depending on the values which contain. We covered a column which contains lists and also splitting values separated by delimiter.

Below you can find some useful resources:

- [Notebook](https://github.com/softhints/Pandas-Tutorials/blob/master/split/split-pandas-list-column-into-multiple-columns.ipynb?ref=datascientyst.com)
- [pandas.Series.str.split](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html?ref=datascientyst.com)
- [pandas.Series.tolist](https://pandas.pydata.org/docs/reference/api/pandas.Series.tolist.html?ref=datascientyst.com)