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.
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)
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3