Home > Net >  Pandas Price Analysis
Pandas Price Analysis

Time:01-03

Good morning, I need help, I need to create an analysis that brings me the lowest value per line, and which supplier is the best value.

import pandas as pd
data = {'CODBAR':[29951,29952,29953],'DESCRI':['IPHONE 15','SAMSUNG S40','MOTOROLA G1000'],'PRICE_PROVIDER1':[1000.00,1200.00,1100.00],'PRICE_PROVIDER2':[999.00,1299.00,1199.00],'PRICE_PROVIDER3':[1199.00,1199.00,999.00]}
df = pd.DataFrame(data)
df.head()
CODBAR  DESCRI  PRICE_PROVIDER1 PRICE_PROVIDER2 PRICE_PROVIDER3
0   29951   IPHONE 15   1000.0  999.0   1199.0
1   29952   SAMSUNG S40 1200.0  1299.0  1199.0
2   29953   MOTOROLA G1000  1100.0  1199.0  999.0

I would like to find the lowest value line by line, and which supplier has the lowest value, and create a new dataframe with this data, so that we can proceed with the purchase order. If anyone has any ideas, I'd be very grateful.

CodePudding user response:

You can do this way using idxmin to find the column with the minimum value for each row and min to find the minimum value for each row.

import pandas as pd

data = {'CODBAR':[29951,29952,29953],'DESCRI':['IPHONE 15','SAMSUNG S40','MOTOROLA G1000'],'PRICE_PROVIDER1':[1000.00,1200.00,1100.00],'PRICE_PROVIDER2':[999.00,1299.00,1199.00],'PRICE_PROVIDER3':[1199.00,1199.00,999.00]}

df = pd.DataFrame(data)

# Select only the numerical columns
df_num = df.select_dtypes(include=['float64'])

# Find the column with the minimum value for each row
min_col = df_num.idxmin(axis=1)

# Find the minimum value itself
min_val = df_num.min(axis=1)

# Create a new dataframe with the CODBAR, DESCRI, and minimum value and provider
result = pd.DataFrame({'CODBAR': df['CODBAR'], 'DESCRI': df['DESCRI'], 'MIN_VALUE': min_val, 'PROVIDER': min_col})

print(result)

Output:

   CODBAR          DESCRI  MIN_VALUE         PROVIDER
0   29951       IPHONE 15      999.0  PRICE_PROVIDER2
1   29952     SAMSUNG S40     1199.0  PRICE_PROVIDER3
2   29953  MOTOROLA G1000      999.0  PRICE_PROVIDER3

CodePudding user response:

You can find several aggregates at once. You can throw in count, sum, mean, stdev, etc. Here is min & max.

import pandas as pd

data = {'CODBAR':[29951,29952,29953],'DESCRI':['IPHONE 15','SAMSUNG S40','MOTOROLA G1000'],'PRICE_PROVIDER1':[1000.00,1200.00,1100.00],'PRICE_PROVIDER2':[999.00,1299.00,1199.00],'PRICE_PROVIDER3':[1199.00,1199.00,999.00]}

df = pd.DataFrame(data)
df

df[['PRICE_PROVIDER1', 'PRICE_PROVIDER2', 'PRICE_PROVIDER3']].agg(['min', 'max'])

enter image description here

  • Related