i have multiple dataframe columns which look like this:
Day1
0 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
1 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
2 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
3 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
4 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
What i want is that every character is seperated in a own column:
012345678910111213....
0 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
1 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
2 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
3 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
4 DDDDDDDDDDBBBBBBAAAAAAAAAABBBBBBDDDDDDDDDDDDDDDD
So that "Day 1-Column" is splitted in 48 Columns and every Column has one of the Value A/B/C/D
i tried with split, but that didnt work.
Split column by delimiter into multiple columnsApply the pandas series str. split() function on the “Address” column and pass the delimiter (comma in this case) on which you want to split the column. Also, make sure to pass True to the expand parameter.
We can use the pandas Series. str. split() function to break up strings in multiple columns around a given separator or delimiter. It's similar to the Python string split() method but applies to the entire Dataframe column.
split() function is used to break up single column values into multiple columns based on a specified separator or delimiter. The Series. str. split() function is similar to the Python string split() method, but split() method works on the all Dataframe columns, whereas the Series.
The str. split() function is used to split strings around given separator/delimiter. The function splits the string in the Series/Index from the beginning, at the specified delimiter string. Equivalent to str.
You can call apply
and for each row call pd.Series
on the the list
of the values:
In [16]:
df['Day1'].apply(lambda x: pd.Series(list(x)))
Out[16]:
0 1 2 3 4 5 6 7 8 9 ... 38 39 40 41 42 43 44 45 46 47
0 D D D D D D D D D D ... D D D D D D D D D D
1 D D D D D D D D D D ... D D D D D D D D D D
2 D D D D D D D D D D ... D D D D D D D D D D
3 D D D D D D D D D D ... D D D D D D D D D D
4 D D D D D D D D D D ... D D D D D D D D D D
[5 rows x 48 columns]
It looks like you have trailing spaces, remove these using str.rstrip
:
df['Day1'] = df['Day1'].str.rstrip()
then do the above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With