简体   繁体   English

在 python 中,如何计算 3 个或更多数据帧的 MAX 和 MIN

[英]in python , how to calculate MAX and MIN for 3 or more dataframes

How to calculate MAX and MIN for 3 or more dataframes如何计算 3 个或更多数据帧的 MAX 和 MIN
I can do calculate the difference between prices simply by adding this line of code我可以通过添加这行代码来计算价格之间的差异

diff['list2-list1'] = diff['price2'] - diff['price1']

but it not work to calculate MIN with但是用 MIN 计算是行不通的

diff['min'] =  (df1,df2.df3).min()

or或者

diff['min'] = (diff['price2'],diff['price1'],diff['price3']).min()

or或者

diff['min'] = (diff['price2'],diff['price1'],diff['price3']).idxmin()

and do not print result of if in new column when latest list (list3) have minimum value并且当最新列表(list3)具有最小值时,不要在新列中打印if的结果

if diff['min'] == diff['price3']
     diff['Lowest now?'] = "yes"

The python code I have我有 python 代码

import pandas 
import numpy as np
import csv
from csv_diff import load_csv, compare



df1 = pandas.read_csv('list1.csv')
df1['version'] = 'list1'

df2 = pandas.read_csv('list2.csv')
df2['version'] = 'list2'

df3 = pandas.read_csv('list3.csv')
df3['version'] = 'list3'

# keep only columns 'version', 'ean', 'price'
diff = df1.append([df2,df3])[['version', 'ean','price']]


# keep only duplicated eans, which will only occur
# for eans in both original lists
diff = diff[diff['ean'].duplicated(keep=False)]
# perform a pivot https://pandas.pydata.org/pandas-docs/stable/user_guide/reshaping.html
diff = diff.pivot_table(index='ean', columns='version', values='price', aggfunc='first')

# back to a normal dataframe
diff = diff.reset_index()
diff.columns.name = None

# rename columns and keep only what we want
diff = diff.rename(columns={'list1': 'price1', 'list2': 'price2', 'list3': 'price3'})[['ean', 'price1', 'price2','price3']]
diff['list2-list1'] = diff['price2'] - diff['price1']
diff['list3-list2'] = diff['price3'] - diff['price2']

diff['min'] =  (df1,df2).min()
if diff['min'] == diff['price3']
     diff['Lowest now?'] = "yes"

diff.to_csv('diff.csv')

more information更多信息

headers of list1,lsit2,list3 are the same list1,lsit2,list3 的标题相同

price,ean,unit 

example of list1列表 1 的示例

price,ean,unit 
143.80,2724316972629,0
125.00,2724456127521,0
158.00,2724280705919,0
19.99,2724342954019,0
20.00,2724321942662,0
212.00,2724559841560,0
1322.98,2724829673686

example of list2清单 2 的示例

price,ean,unit 
55.80,2724316972629,0
15.00,2724456127521,0
66.00,2724559841560,0
1622.98,2724829673686,0

example of list3清单 3 的示例

price,ean,unit 
139.99,2724342954019,0
240.00,2724321942662,0
252.00,2724559841560,0
1422.98,2724829673686,0

There you go:你有 go:

data = pd.concat([df1, df2, df3], axis=1).fillna(0).astype('float')
data['minimum_price'] = data['price'].min(1)
data['maximum_price'] = data['price'].max(1)

Out:出去:

     price           ean  units    price           ean  units    price           ean  units  minimum_price  maximum_price
0   143.80  2.724317e+12    0.0    55.80  2.724317e+12    0.0   139.99  2.724343e+12    0.0          55.80         143.80
1   125.00  2.724456e+12    0.0    15.00  2.724456e+12    0.0   240.00  2.724322e+12    0.0          15.00         240.00
2   158.00  2.724281e+12    0.0    66.00  2.724560e+12    0.0   252.00  2.724560e+12    0.0          66.00         252.00
3    19.99  2.724343e+12    0.0  1622.98  2.724830e+12    0.0  1422.98  2.724830e+12    0.0          19.99        1622.98
4    20.00  2.724322e+12    0.0     0.00  0.000000e+00    0.0     0.00  0.000000e+00    0.0           0.00          20.00
5   212.00  2.724560e+12    0.0     0.00  0.000000e+00    0.0     0.00  0.000000e+00    0.0           0.00         212.00
6  1322.98  2.724830e+12    0.0     0.00  0.000000e+00    0.0     0.00  0.000000e+00    0.0           0.00        1322.98

Assuming the dataframes have the same columns, you can use pd.concat .假设数据框具有相同的列,您可以使用pd.concat

min = pd.concat(df1, df2,df3).min() . min = pd.concat(df1, df2,df3).min()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM