> ## 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 Data Based on Condition in Pandas
- URL: https://datascientyst.com/how-to-split-column-data-based-on-condition-in-pandas/
- Published: 2025-02-17T21:53:01.000Z
- Updated: 2025-02-17T21:53:01.000Z
- Author: John D K
- Tags: split()

In this short guide, I'll show you **how to split a column based on condition in Pandas DataFrame.** The example will show how to split a column containing URLs and extract only the last two parts (domain and top-level domain - TLD)

**(1) Quick Solution Using `.` as a Separator**

```python
df['domain'] = df['url'].apply(lambda x: '.'.join(x.split('.')[-2:]))

```

**(2) Using `rsplit()` for a More Efficient Split**

```python
df['domain'] = df['url'].str.rsplit('.', n=2).str[-2:].str.join('.')

```

**(3) Using lamdba for conditional split**

```python
def find_value_column(row):
    if row.url.count('.') == 2:
        return row['url'].split('.', 1)[1]
    else:
        return row['url']

df['domain'] = df.apply(find_value_column, axis=1)

```

![](https://datascientyst.com/content/images/2025/02/how-to-split-column-data-based-on-condition-in-pandas.webp)

## 1: Example DataFrame with URLs

Let's create a DataFrame with URLs containing one or two dots:

```python
import pandas as pd

# Sample data
data = {
    'url': ['example.com', 'www.example.com', 'test.org', 'blog.test.org']
}

df = pd.DataFrame(data)

```

### **Output:**

|   | url             |
| - | --------------- |
| 0 | example.com     |
| 1 | www.example.com |
| 2 | test.org        |
| 3 | blog.test.org   |

## 2: Extracting the Netloc and Domain

To keep only the last two parts of the domain, we can use `split('.')` and take the last two elements:

```python
df['domain'] = df['url'].apply(lambda x: '.'.join(x.split('.')[-2:]))

```

### **Output:**

|   | url             | domain      |
| - | --------------- | ----------- |
| 0 | example.com     | example.com |
| 1 | www.example.com | example.com |
| 2 | test.org        | test.org    |
| 3 | blog.test.org   | test.org    |

## 3: Optimized Solution Using `rsplit()`

A more efficient approach is using `rsplit()`, which splits from the right and limits the number of splits:

```python
df['domain'] = df['url'].str.rsplit('.', n=2).str[-2:].str.join('.')

```

This method avoids unnecessary splits and is faster for large datasets.

## Conclusion

In this guide, we learned how to:

- Extract the last two parts of a domain name
- Use `split('.')` with `apply()` for flexible extraction
- Use `rsplit()` for a more optimized approach

## Resources

- [Pandas .str.split() Documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html?ref=datascientyst.com)
- [Pandas .apply() Function](https://pandas.pydata.org/docs/reference/api/pandas.Series.apply.html?ref=datascientyst.com)
- [Pandas .rsplit() for Right Splitting](https://pandas.pydata.org/docs/reference/api/pandas.Series.str.rsplit.html?ref=datascientyst.com)