Issue
I have a NumPy array of the form (3*1):
['1 2 3', '4 5 6 7 8 9', '10 11 12 13'].
Each row is a collection of feature values and each row has a different number of features. How can I split this array into multiple columns with each column representing a feature for all rows?
Update:
I am now using on DataFrames. So, I have a data frame with one column. Want to expand it and also convert it to integers. How can I do that?
Expectation: I want to split data in each row into multiple columns (features). Then, each column to numerics.
Solution
You can use expand=True
in str.split
like below:
df = pd.DataFrame({'features':['191 367 614 634 711',
'1202 1220 131 1730 2281 2572 2602 2611 2824',
'2855 2940 3149 3313 3560 3568 3824 4185 4266']})
df.features.str.split(expand=True).astype(float).add_prefix('feature_')
Output:
feature_0 feature_1 feature_2 ... feature_6 feature_7 feature_8
0 191.0 367.0 614.0 ... NaN NaN NaN
1 1202.0 1220.0 131.0 ... 2602.0 2611.0 2824.0
2 2855.0 2940.0 3149.0 ... 3824.0 4185.0 4266.0
Answered By – I'mahdi
Answer Checked By – Senaida (BugsFixing Volunteer)