Split Pandas Series into DataFrame by delimiter

O.rka

I'm trying to split a pandas series object by a particular delimiter "; " in this case. I want to turn it into a dataframe there will always be the same amount of "columns" or to be more exact, same amount of "; " that will indicate columns. I thought this would do the trick but it didnt python, how to convert a pandas series into a pandas DataFrame? I dont want to iterate through, I'm sure pandas has made a shortcut that's more effective.

Does anyone know of the most efficient way to split this series into a dataframe by "; " ?

#Example Data
SR_test = pd.Series(["a; b; c; d; e","aa; bb; cc; dd; ee","a1; b2; c3; d4; e5"])
# print(SR_test)
# 0         a; b; c; d; e
# 1    aa; bb; cc; dd; ee
# 2    a1; b2; c3; d4; e5

#Convert each row one at a time (not efficient)
tmp = []
for element in SR_test:
    tmp.append([e.strip() for e in element.split("; ")])
DF_split = pd.DataFrame(tmp)
# print(DF_split)
#     0   1   2   3   4
# 0   a   b   c   d   e
# 1  aa  bb  cc  dd  ee
# 2  a1  b2  c3  d4  e5
jezrael

You can use str.split:

df = SR_test.str.split('; ', expand=True)
print df

    0   1   2   3   4
0   a   b   c   d   e
1  aa  bb  cc  dd  ee
2  a1  b2  c3  d4  e5

Another faster solution, if Series have no NaN values:

print pd.DataFrame([ x.split('; ') for x in SR_test.tolist() ])
    0   1   2   3   4
0   a   b   c   d   e
1  aa  bb  cc  dd  ee
2  a1  b2  c3  d4  e5

Timings:

SR_test = pd.concat([SR_test]*1000).reset_index(drop=True)

In [21]: %timeit SR_test.str.split('; ', expand=True)
10 loops, best of 3: 34.5 ms per loop

In [22]: %timeit pd.DataFrame([ x.split('; ') for x in SR_test.tolist() ])
100 loops, best of 3: 9.59 ms per loop

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 Pandas Series into DataFrame by delimiter

From Java

split multiple columns in pandas dataframe by delimiter

From Dev

How to split a pandas dataframe or series by day (possibly using an iterator)

From Dev

Split a Pandas Dataframe into multiple Dataframes based on Triangular Number Series

From Dev

How to save a pandas dataframe such that there is no delimiter?

From Dev

Splitting a pandas dataframe column by delimiter

From Dev

How to save a pandas dataframe such that there is no delimiter?

From Dev

Split dataframe column with second column as delimiter

From Dev

Pandas Series str.split assign the split result back to the original dataframe as separate columns

From Dev

Python Pandas split list by delimiter into own columns

From Dev

Pandas dataframe from series of series

From Dev

Split time-series dataframe

From Dev

Split Dataframe column on delimiter when number of strings to split is not definite

From Java

Convert pandas Series to DataFrame

From Dev

Pandas merging a Dataframe and a series

From Dev

concat a DataFrame with a Series in Pandas

From Dev

Comparing pandas DataFrame to Series

From Dev

dataframe pandas subset series

From Dev

pandas dataframe to series

From Dev

Adding series to pandas dataframe

From Dev

Python Pandas Series to Dataframe

From Dev

Pandas Series split n times

From Dev

Search pandas series for value and split series at that value

From Java

Split a large pandas dataframe

From Dev

Split pandas dataframe index

From Dev

pandas dataframe split on condition

From Dev

.gz file to pandas DataFrame with hive delimiter

From Dev

Load .csv with unknown delimiter into Pandas DataFrame

From Dev

How to add column delimiter to Pandas dataframe display

Related Related

  1. 1

    Split Pandas Series into DataFrame by delimiter

  2. 2

    split multiple columns in pandas dataframe by delimiter

  3. 3

    How to split a pandas dataframe or series by day (possibly using an iterator)

  4. 4

    Split a Pandas Dataframe into multiple Dataframes based on Triangular Number Series

  5. 5

    How to save a pandas dataframe such that there is no delimiter?

  6. 6

    Splitting a pandas dataframe column by delimiter

  7. 7

    How to save a pandas dataframe such that there is no delimiter?

  8. 8

    Split dataframe column with second column as delimiter

  9. 9

    Pandas Series str.split assign the split result back to the original dataframe as separate columns

  10. 10

    Python Pandas split list by delimiter into own columns

  11. 11

    Pandas dataframe from series of series

  12. 12

    Split time-series dataframe

  13. 13

    Split Dataframe column on delimiter when number of strings to split is not definite

  14. 14

    Convert pandas Series to DataFrame

  15. 15

    Pandas merging a Dataframe and a series

  16. 16

    concat a DataFrame with a Series in Pandas

  17. 17

    Comparing pandas DataFrame to Series

  18. 18

    dataframe pandas subset series

  19. 19

    pandas dataframe to series

  20. 20

    Adding series to pandas dataframe

  21. 21

    Python Pandas Series to Dataframe

  22. 22

    Pandas Series split n times

  23. 23

    Search pandas series for value and split series at that value

  24. 24

    Split a large pandas dataframe

  25. 25

    Split pandas dataframe index

  26. 26

    pandas dataframe split on condition

  27. 27

    .gz file to pandas DataFrame with hive delimiter

  28. 28

    Load .csv with unknown delimiter into Pandas DataFrame

  29. 29

    How to add column delimiter to Pandas dataframe display

HotTag

Archive