Im getting csv file with many pieces with length/width dimensions and need to find out the smallest box l/w. First thoughts was to transpose(sort) between pandas df columns(like this) to collect the biggest value of both in first column, so this biggest value would be one side of box and the biggest of second column would be another side of box:
length | width |
---|---|
200 | 150 |
100 | 300 |
transpose to this
length | width |
---|---|
300 | 100 |
200 | 150 |
So box would be 300x150. But Im getting error for example where I linked above. Also it possible to sort in nested list in same way. So questions:
- What is the best way(pythonic) to store(manipulate) data of 2(3) dimensions l/w(thickness).
- Pretty sure there will be more elegant way to solve this, so please show examples or link where I could read because dont know how to search correctly.
- How sort values in pandas between same row but different columns. Thx.
CodePudding user response:
You can use numpy to sort and make a new dataframe:
import numpy as np
pd.DataFrame(np.sort(np.sort(df.values, axis=1)[:, ::-1], axis=0),
columns=df.columns)
output:
length width
1 300 100
0 200 150
You can also use:
(df.apply(sorted,
reverse=True,
axis=1,
result_type='expand')
.rename(columns=dict(zip(range(len(df.columns)), df.columns)))
.sort_values(cols, ascending=False) # optional if the goal is to get the max per column
)
output:
length width
1 300 100
0 200 150
CodePudding user response:
Merge your columns as a sorted tuple and sort values at the end:
cols = ['length', 'width']
f = lambda x: sorted(tuple(x), reverse=True)
df[cols] = df[cols].apply(f, axis=1, result_type='expand')
df = df.sort_values(cols)
Output:
>>> df
length width
0 200 150
1 300 100