I have a DataFrame that looks like this:
|Index| Dimension |
|-----|-----------|
|0 |1@43X32X34 |
|1 |1@120X80X74|
|2 |2@26X26X32 |
|3 |1@120X80X81|
I want to extract the number from the Dimension column and split it into multiple column:
|Index| Amount|Length|Width|Height|
|-----|-------|------|-----|------|
|0 | 1| 43| 32| 34|
|1 | 1| 120| 80| 74|
|2 | 2| 26| 26| 32|
|3 | 1| 120| 80| 81|
How to do that using the Pandas module in Python? Thank you!
Pandas series aka columns has a unique() method that filters out only unique values from a column. The first output shows only unique FirstNames. We can extend this method using pandas concat() method and concat all the desired columns into 1 single column and then find the unique of the resultant column.
From the dataframe we select the column “Name” using a [] operator that returns a Series object and uses Series. Values to get a NumPy array from the series object. Next, we will use the function tolist() provided by NumPy array to convert it to a list.
You can use pandas str split with expand=True, the delimiters are @ and X, so passing them in will ensure appropriate splits. You can then insert Index as the first column and rewrite the column names:
M = df.Dimension.str.split('[@X]',expand=True)
M.insert(0,'Index',df.Index)
M.columns = ['Index','Amount','Length','Width','Height']
Index Amount Length Width Height
0 0 1 43 32 34
1 1 1 120 80 74
2 2 2 26 26 32
3 3 1 120 80 81
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