Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract multiple numbers from Pandas Dataframe

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!

like image 681
Doggy Face Avatar asked Mar 01 '20 04:03

Doggy Face


People also ask

How extract unique values from multiple columns in pandas?

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.

How do I get a list of values from a DataFrame?

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.


1 Answers

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
like image 198
sammywemmy Avatar answered Sep 22 '22 08:09

sammywemmy