Pandas DataFrame efficiently split one column into multiple

Rory LM

I have a dataframe similar to this:

data = {"col_1": [0, 1, 2],
        "col_2": ["abc", "defg", "hi"]}
df = pd.DataFrame(data)

Visually:

   col_1 col_2
0      0   abc
1      1   defg
2      2   hi

What I'd like to do is split up each character in col_2, and append it as a new column to the dataframe

example iterative method:

def get_chars(string):
    chars = []
    for char in string:
        chars.append(char)
    return chars

char_df = pd.DataFrame()
for i in range(len(df)):
    char_arr = get_chars(df.loc[i, "col_2"])
    temp_df = pd.DataFrame(char_arr).T
    char_df = pd.concat([char_df, temp_df], ignore_index=True, axis=0)

df = pd.concat([df, char_df], ignore_index=True, axis=1)

Which results in the correct form:

   0     1  2  3    4    5
0  0   abc  a  b    c  NaN
1  1  defg  d  e    f    g
2  2    hi  h  i  NaN  NaN

But I believe iterating though the dataframe like this is very inefficient, so I want to find a faster (ideally vectorised) solution.

In reality, I'm not really splitting up strings, but the point of this question is to find a way to efficiently process one column, and return many.

jezrael

If need performance use DataFrame constructor with convert values to lists:

df = df.join(pd.DataFrame([list(x) for x in df['col_2']], index=df.index))

Or:

df = df.join(pd.DataFrame(df['col_2'].apply(list).tolist(), index=df.index))

print (df)
   col_1 col_2  0  1     2     3
0      0   abc  a  b     c  None
1      1  defg  d  e     f     g
2      2    hi  h  i  None  None

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Split list in Pandas dataframe column into multiple columns

From Dev

pandas: Split separated values in a DataFrame column (one Series) into multiple Columns. Elegant solutions?

From Dev

Split one column to two columns depending one the content in pandas dataframe

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

How to multiply every column of one Pandas Dataframe with every column of another Dataframe efficiently?

From Dev

Pandas split column into multiple

From Dev

Pandas split column into multiple

From Java

Pandas dataframe Split One column data into 2 using some condition

From Dev

DataFrame Split On Rows and apply on header one column using Python Pandas

From Dev

How to split a dataframe column into multiple columns with a Pandas converter

From Dev

Split a text(with names and values) column into multiple columns in Pandas DataFrame

From Dev

Need to split variable length data in a pandas dataframe column into multiple columns

From Dev

Reordering pandas dataframe based on multiple column and sum of one column

From Dev

Pandas efficiently normalize column titles in a dataframe

From Dev

Split pandas dataframe by column variable

From Java

Pandas split DataFrame by column value

From Java

Separate to split one column to multiple

From Dev

Split one column into multiple columns

From Dev

Split one column into multiple columns

From Dev

Assigning multiple column values in a single row of pandas DataFrame, in one line

From Dev

Sorting multiple Pandas Dataframe Columns based on the sorting of one column

From Dev

Split a pandas dataframe into two dataframes efficiently based on some condition

From Dev

Efficiently replace values from a column to another column Pandas DataFrame

From Dev

Pandas: Unstacking One Column of a DataFrame

From Dev

Shuffle one column in pandas dataframe

From Dev

Pandas: Unstacking One Column of a DataFrame

From Dev

Reshaping dataframe on one column in pandas

From Dev

Split pandas dataframe column based on number of digits

Related Related

  1. 1

    Split list in Pandas dataframe column into multiple columns

  2. 2

    pandas: Split separated values in a DataFrame column (one Series) into multiple Columns. Elegant solutions?

  3. 3

    Split one column to two columns depending one the content in pandas dataframe

  4. 4

    Pandas, DataFrame: Splitting one column into multiple columns

  5. 5

    Pandas, DataFrame: Splitting one column into multiple columns

  6. 6

    How to multiply every column of one Pandas Dataframe with every column of another Dataframe efficiently?

  7. 7

    Pandas split column into multiple

  8. 8

    Pandas split column into multiple

  9. 9

    Pandas dataframe Split One column data into 2 using some condition

  10. 10

    DataFrame Split On Rows and apply on header one column using Python Pandas

  11. 11

    How to split a dataframe column into multiple columns with a Pandas converter

  12. 12

    Split a text(with names and values) column into multiple columns in Pandas DataFrame

  13. 13

    Need to split variable length data in a pandas dataframe column into multiple columns

  14. 14

    Reordering pandas dataframe based on multiple column and sum of one column

  15. 15

    Pandas efficiently normalize column titles in a dataframe

  16. 16

    Split pandas dataframe by column variable

  17. 17

    Pandas split DataFrame by column value

  18. 18

    Separate to split one column to multiple

  19. 19

    Split one column into multiple columns

  20. 20

    Split one column into multiple columns

  21. 21

    Assigning multiple column values in a single row of pandas DataFrame, in one line

  22. 22

    Sorting multiple Pandas Dataframe Columns based on the sorting of one column

  23. 23

    Split a pandas dataframe into two dataframes efficiently based on some condition

  24. 24

    Efficiently replace values from a column to another column Pandas DataFrame

  25. 25

    Pandas: Unstacking One Column of a DataFrame

  26. 26

    Shuffle one column in pandas dataframe

  27. 27

    Pandas: Unstacking One Column of a DataFrame

  28. 28

    Reshaping dataframe on one column in pandas

  29. 29

    Split pandas dataframe column based on number of digits

HotTag

Archive