Home >Backend Development >Python Tutorial >How to Split a Column of Tuples into Multiple Columns in a Pandas DataFrame?

How to Split a Column of Tuples into Multiple Columns in a Pandas DataFrame?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 04:52:02795browse

How to Split a Column of Tuples into Multiple Columns in a Pandas DataFrame?

Splitting a Column of Tuples in a Pandas DataFrame

In Pandas dataframes, splitting a column containing tuples into multiple columns is a common operation. To achieve this, one can adopt the following methods:

Using pd.DataFrame(col.tolist())

This method converts the tuple column into a list of tuples and then creates a new dataframe from it. The index of the new dataframe matches that of the original.

<code class="python">import pandas as pd

# Create a dataframe with a column containing tuples
df = pd.DataFrame({'a': [1, 2], 'b': [(1, 2), (3, 4)]})

# Split the 'b' column into 'b1' and 'b2'
df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)

# Print the resulting dataframe
print(df)</code>

Output:

   a  b  b1  b2
0  1  (1, 2)   1   2
1  2  (3, 4)   3   4

Note: Using df['b'].apply(pd.Series) instead of pd.DataFrame(df['b'].tolist(), index=df.index) also works. However, it is slower and requires more memory.

The above is the detailed content of How to Split a Column of Tuples into Multiple Columns in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn