Reputation: 343
I have a pandas dataframe FruitsInfo
as shown :
====================================
| Name | Color | Size | No. |
====================================
| Apple | Red | Medium | 20 |
------------------------------------
| Apple | Green | Small | 10 |
------------------------------------
| Apple | Yellow | Small | 5 |
------------------------------------
| Grapes | Green | Small | 50 |
------------------------------------
| Grapes | Purple | Small | 60 |
------------------------------------
| Grapes | Black | Medium | 80 |
------------------------------------
| Mango | Yellow | Medium | 30 |
------------------------------------
| Mango | Green | Small | 25 |
------------------------------------
| Mango | Orange | Small | 20 |
====================================
I want to merge only Name
column as shown below. How can I get this result using pandas ?
====================================
| Name | Color | Size | No. |
====================================
| Apple | Red | Medium | 20 |
| ---------------------------
| | Green | Small | 10 |
| ---------------------------
| | Yellow | Small | 5 |
------------------------------------
| Grapes | Green | Small | 50 |
| ---------------------------
| | Purple | Small | 60 |
| ---------------------------
| | Black | Medium | 80 |
------------------------------------
| Mango | Yellow | Medium | 30 |
| ---------------------------
| | Green | Small | 25 |
| ---------------------------
| | Orange | Small | 20 |
====================================
Upvotes: 0
Views: 961
Reputation: 5745
i dont really understand what do you mean by grouping, but it seems like you got a multi index dataframe the represnted in prints like this so you can do the following:
import pandas as pd
import io
csv = io.StringIO('''| Name | Color | Size | No. |
| Apple | Red | Medium | 20 |
| Apple | Green | Small | 10 |
| Apple | Yellow | Small | 5 |
| Grapes | Green | Small | 50 |
| Grapes | Purple | Small | 60 |
| Grapes | Black | Medium | 80 |
| Mango | Yellow | Medium | 30 |
| Mango | Green | Small | 25 |
| Mango | Orange | Small | 20 |''')
df = pd.read_csv(csv,sep=r'\s*\|\s*').iloc[:,1:-1]
>>>df.set_index(['Name','Color'])
Size No.
Name Color
Apple Red Medium 20
Green Small 10
Yellow Small 5
Grapes Green Small 50
Purple Small 60
Black Medium 80
Mango Yellow Medium 30
Green Small 25
Orange Small 20
Upvotes: 3