0

I know this can be done with pivot, I just don't know how. When I tried, I got too many rows with many nan values. I have a number of objects identified by their x and y coordinates. I want to get one row for each such object, as in this example:

input = pd.DataFrame.from_dict({'dim_name':['height',  'height', 'width', 'width'], 'dim_val': [1, 2, 10, 20], 'x': [0.1, 0.2, 0.1, 0.2], 
                            'y':[0.6, 0.7, 0.6, 0.7]})

output = pd.DataFrame.from_dict({'x':[0.1, 0.2], 'y': [0.6, 0.7], 'height': [1, 2],  'width':[10, 20]})

How do I do this in pandas?

user623949
  • 101
  • 1
  • 7
  • What exactly have you tried? Seems quite straightforward… `df.pivot(index=['x', 'y'], columns='dim_name', values='dim_val').reset_index()` – mozway Apr 27 '22 at 03:11
  • Something of that sort, but I suppose I had the args all at the wrong places, like `x.pivot(index='dim_name', columns=['x', 'y'], values='dim_val').reset_index()` – user623949 Apr 27 '22 at 03:19
  • that's why you should always provide your code (and carefully read the docs) ;) – mozway Apr 27 '22 at 03:20

0 Answers0