mardi 5 octobre 2021

If statement on dataframe

I have two dataframe.

my purpose;

IF 1-) If the place of a code is u10 and its final sorted number is 2>=, delete u10 in "Place" and Create a separate excel and open a column for the "outputs" and write their name there.

IF 2-) If the place of a code is empty(like facebook) and its final sorted number is 3<=, add u10 in "Place" and Create a separate excel and open a column for the "inputs" and write their name there.

IF 3-) Open a columns like "Waitings". After making the adding and exits, put whoever is first in line according to the final sorted number in the waiting list. A maximum of 3 codes will be placed on those who are waiting.

DF;

Code Volume Trade
ApplA 500 1000
Amazon 1000 500
Facebook 250 750
ApplE 100 1500

The other dataframe's name is "buffer".

Name Place
AppLA U30
Amazon U30
AppLA U20
Facebook U10
ApplE U10
Amazon U20
AppLA U10
Amazon row
Facebook U20
Amazon U10
AppLE U20

df_samecompanies['Codes'] 

df['Volume Order'] = df['Volume'].rank(ascending=False)
df['Trade Order']  = df['Trade'].rank(ascending=False)
df['Trade Order2']  = df['Trade'].rank(ascending=True)
df['Max Ordered Number']  = df[['Volume Order', 'Trade Order']].max(axis=1)
df['Final Sorted Number'] = df[['Max Ordered Number', 'Trade Order2']].apply(tuple, axis=1).rank(ascending=False)
df.drop('Trade Order2', axis=1)
buffer_pivot = buffer.pivot(index='Name', columns='Place', values='Place').add_prefix('PLACE ')
df = df.set_index('Code').join(buffer_pivot).reset_index()
print(df)

    Code  Volume  Trade  Volume Order  Trade Order  Max Ordered Number  Final Sorted Number PLACE U10 PLACE U20 PLACE U30
0     ApplG     500   1000           2.0          2.0                 2.0                  10.0       NaN       NaN       NaN
1     ApplF     500   1000           2.0          2.0                 2.0                  9.0       NaN       NaN       NaN
2     ApplE     500   1000           2.0          2.0                 2.0                  8.0       NaN       NaN       NaN
3     ApplD     500   1000           2.0          2.0                 2.0                  7.0       NaN      NaN      NaN
4     ApplC     500   1000           2.0          2.0                 2.0                  6.0       NaN       NaN       NaN
5     ApplB     500   1000           2.0          2.0                 2.0                  5.0       NaN       NaN       NaN
6     ApplA     500   1000           2.0          2.0                 2.0                  4.0       U10       U20       U30
7    Amazon    1000    500           1.0          4.0                 4.0                  2.0       U10       U20       U30
8  Facebook     250    750           3.0          3.0                 3.0                  3.0       U10       U20       NaN
9     ApplE     100   1500           4.0          1.0                 4.0                  1.0       U10       U20       NaN

 ```



my expected outputs;

Seperate excel and open columns(name is not important.)
| CODE| OUTPUTS | 
|:---- |:------:| 
| Amazon  | U10    
| Facebook  | U10    
| ApplA   | U10  


Total outputs: 

U10= 3


| CODE| ADDİNGS | 
|:---- |:------:|   


Total addings: 

None

Differences;

U10 = 3 ADDİNGS NEED



in this example; addings and outputs are not equal for U10 .We have to need equal it. **Thats' why; If the number of adding and exiting codes is not equal, add or subtract whoever is next according to the final sorted number.**


| CODE| MORE ADDINGS | 
|:---- |:------:|   
| ApplB  | U10  
| ApplC  | U10  
| ApplD  | U10  


My final step;

3-) Open a columns like "Waitings". After making the adding and exits, put whoever is first in line according to the final sorted number in the waiting list. A maximum of **3 codes** will be placed on those who are waiting.



| CODE| Waitings | 
|:---- |:------:|   
| ApplE  | U10
| ApplF  | U10
| ApplG  | U10


Regards,




Aucun commentaire:

Enregistrer un commentaire