Pandas价格分析

lstz6jyr  于 2023-01-07  发布在  其他
关注(0)|答案(2)|浏览(152)

早上好,我需要帮助,我需要创建一个分析,为我带来最低的每行价值,以及哪个供应商是最好的价值。

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

我想逐行找出最低值,以及哪个供应商的值最低,然后用这些数据创建一个新的数据框,这样我们就可以继续处理采购订单了。如果有人有任何想法,我将非常感谢。

uyto3xhc

uyto3xhc1#

您可以使用idxmin查找每行具有最小值的,使用min查找每行的最小

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)
    • 输出:**
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
zbq4xfa0

zbq4xfa02#

你可以一次找到几个聚合体,你可以加入计数、总和、平均值、标准差等,这里是最小值和最大值。

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'])

相关问题