Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting by data in another Dataframe

I've been stuck with an engineering problem thats Python/Pandas related. I'd appreciate any help given. I've simplified the numbers so I can better explain myself. I have something similar to the following:

positioning(x-axis) Calculated difference
1 0.25 0.05
2 0.75 0.06
3 1.25 0.02
4 0.25 0.05
5 0.75 0.05
6 1.25 0.02
7 0.25 0.09
8 0.75 0.01
9 1.25 0.02
10 0.25 0.05

What I need to do is re-organise the calculated difference based on the x-axis positioning. So it looks something like this:

(0.25) (0.75) (1.25)
0.05 0 0
0 0.06 0
0 0 0.02
0.5 0 0
0 0.5 0
0 0 0.02
0.09 0 0
0 0.01 0
0 0 0.02
0.05 0 0

As you can see, I need to organize everything based on the x-positioning. What is the best approach to this problem? Keep in mind I have 2000+ rows and the x positioning is dynamic but I'm currently working till up to 50(so a lot of columns).

I hope I've clarified the question.

like image 697
apprentice engineer Avatar asked Mar 21 '21 15:03

apprentice engineer


People also ask

How do you sort a DataFrame based on another DataFrame?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.

How do you sort values in Groupby Pandas?

Sort Values in Descending Order with Groupby You can sort values in descending order by using ascending=False param to sort_values() method. The head() function is used to get the first n rows. It is useful for quickly testing if your object has the right type of data in it.

How do I sort a column by DF?

You can sort by column values in pandas DataFrame using sort_values() method. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.

How do I order Pandas DataFrame?

In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.


2 Answers

Use pd.get_dummies:

In [10]: pd.get_dummies(df['positioning(x-axis)']).mul(df['Calculated difference'],axis=0)
Out[10]: 
    0.25  0.75  1.25
1   0.05  0.00  0.00
2   0.00  0.06  0.00
3   0.00  0.00  0.02
4   0.05  0.00  0.00
5   0.00  0.05  0.00
6   0.00  0.00  0.02
7   0.09  0.00  0.00
8   0.00  0.01  0.00
9   0.00  0.00  0.02
10  0.05  0.00  0.00
like image 87
Mayank Porwal Avatar answered Sep 19 '22 14:09

Mayank Porwal


Just do pivot

df.pivot(columns='positioning(x-axis)',values='Calculated difference').fillna(0)
Out[363]: 
Calculated  0.25  0.75  1.25
0           0.05  0.00  0.00
1           0.00  0.06  0.00
2           0.00  0.00  0.02
3           0.05  0.00  0.00
4           0.00  0.05  0.00
5           0.00  0.00  0.02
6           0.09  0.00  0.00
7           0.00  0.01  0.00
8           0.00  0.00  0.02
9           0.05  0.00  0.00
like image 22
BENY Avatar answered Sep 18 '22 14:09

BENY