splitting a column and assigning header in pandas dataframe

Prashant Rai

I have a pandas data frame where i want to split every element in the first column and assign a column header to both the new columns. right now the column has no header.it is something like:

                       0    1   2   3   4

A|Item Name           25   26  31  40  45
B|Item Name           26   28  29  32  50
C|Item Name           31   32  12  32  11

Expected output

 Ltr     Itm                  0    1   2   3   4

 A    Item Name              25   26  31  40  45
 B    Item Name              26   28  29  32  50
 C    Item Name              31   32  12  32  11

used the following code to try and convert the same:

df2 = pd.DataFrame(df2.row.str.split('|', 1).tolist(),
                   columns=['Let', 'Itm'])

but got error that there is no 'row' attribute in the dataframe.

any help is much appreciated. thanks.

Vaishali

If it is an index, you can use

df.index = df.index.str.split('|', expand = True)
df = df.reset_index().rename(columns = {'level_0': 'Ltr', 'level_1': 'Itm'})

You get

    Ltr Itm         0   1   2   3   4
0   A   Item Name   25  26  31  40  45
1   B   Item Name   26  28  29  32  50
2   C   Item Name   31  32  12  32  11

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Convert row to column header for Pandas DataFrame,

From Java

Assigning column name to Pandas Series

From Java

Assigning rank 2 numpy array to pandas DataFrame column behaves inconsistently

From Dev

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

From Dev

Splitting a dataframe based in a column

From Dev

assigning NumericVector to a column of DataFrame

From Dev

Read multiple *.txt files into Pandas Dataframe with filename as column header

From Dev

Splitting a dataframe by column name indices

From Dev

Splitting and unpivoting a column in pandas

From Dev

assigning column names to a pandas series

From Dev

Splitting a List inside a Pandas DataFrame

From Dev

Pandas creates DataFrame with first header column in it's own row

From Dev

Splitting a dataframe based on column values

From Dev

Length mismatch error when assigning new column labels in pandas dataframe

From Dev

Removing header column from pandas dataframe

From Dev

Splitting a pandas dataframe column by delimiter

From Dev

Splitting DataFrame rows with pandas

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

Assigning rank 2 numpy array to pandas DataFrame column behaves inconsistently

From Dev

splitting a column into multiple columns with specific name in pandas dataframe

From Dev

assigning NumericVector to a column of DataFrame

From Dev

Splitting a dataframe based on column values

From Dev

Calculating percentile on pandas dataframe and assigning binary value to new column

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

Splitting a Pandas DataFrame column into two columns

From Dev

Group rows in dataframe by assigning values as a column in pandas dataframe

From Dev

Splitting a Dictionary of tuples into a pandas dataframe

From Dev

Splitting a pandas Dataframe into separate groups

From Dev

Splitting string inside pandas dataframe

Related Related

  1. 1

    Convert row to column header for Pandas DataFrame,

  2. 2

    Assigning column name to Pandas Series

  3. 3

    Assigning rank 2 numpy array to pandas DataFrame column behaves inconsistently

  4. 4

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

  5. 5

    Splitting a dataframe based in a column

  6. 6

    assigning NumericVector to a column of DataFrame

  7. 7

    Read multiple *.txt files into Pandas Dataframe with filename as column header

  8. 8

    Splitting a dataframe by column name indices

  9. 9

    Splitting and unpivoting a column in pandas

  10. 10

    assigning column names to a pandas series

  11. 11

    Splitting a List inside a Pandas DataFrame

  12. 12

    Pandas creates DataFrame with first header column in it's own row

  13. 13

    Splitting a dataframe based on column values

  14. 14

    Length mismatch error when assigning new column labels in pandas dataframe

  15. 15

    Removing header column from pandas dataframe

  16. 16

    Splitting a pandas dataframe column by delimiter

  17. 17

    Splitting DataFrame rows with pandas

  18. 18

    Pandas, DataFrame: Splitting one column into multiple columns

  19. 19

    Assigning rank 2 numpy array to pandas DataFrame column behaves inconsistently

  20. 20

    splitting a column into multiple columns with specific name in pandas dataframe

  21. 21

    assigning NumericVector to a column of DataFrame

  22. 22

    Splitting a dataframe based on column values

  23. 23

    Calculating percentile on pandas dataframe and assigning binary value to new column

  24. 24

    Pandas, DataFrame: Splitting one column into multiple columns

  25. 25

    Splitting a Pandas DataFrame column into two columns

  26. 26

    Group rows in dataframe by assigning values as a column in pandas dataframe

  27. 27

    Splitting a Dictionary of tuples into a pandas dataframe

  28. 28

    Splitting a pandas Dataframe into separate groups

  29. 29

    Splitting string inside pandas dataframe

HotTag

Archive