嵌套循环计数相同值的几倍

时间:2018-08-31 09:49:12

标签: python loops for-loop dataframe nested-loops

我正在尝试创建一个嵌套循环,以计算Car ModelName之间的价格组合,例如:

fact_date     CarID   Country  Type   ManufactureNameSum      Sum_Costs
2017-07-14    1       USA      Car    Ford_Focus____VW_Jetta  34003

这意味着我具有 ManufactorName ModelName 以及 AcquisitionPrice 的总和。由于嵌套循环,以某种方式我收到了两次结果,但只想得到一次。我正在考虑用以下代码来解决它:

row['ManufactureName'] != row2['ManufactureName']

但这只能解决具有相同 ManufactureName 的问题,但不能解决由于两个列表而导致的结果两倍的问题。

希望有人可以在这里提供帮助。

调整

我调整了嵌套循环的代码,但仍然像以前一样存在关于fact_date索引的问题。

调整后的代码

import pandas as pd

df = pd.read_csv('C:/Sales_Cars.csv', encoding='cp1252', sep=';', index_col=0).dropna()
df2 = pd.DataFrame([])
for current_date in df.index.unique():
    for i in range(0, len(df)):
        for j in range(i+1, len(df)):
            if (
                    df.iloc[i]['ManufactureName'] != df.iloc[j]['ManufactureName'] and
                    df.iloc[i]['CarID'] == df.iloc[j]['CarID'] and
                    df.iloc[i]['Country'] == df.iloc[j]['Country']):
                df2 = df2.append(
                    pd.DataFrame({
                        'CarID': df.iloc[i]['CarID'],
                        'Country': df.iloc[i]['Country'],
                        'Type': df.iloc[i]['Type'],
                        'ManufactureNameSum': (
                            df.iloc[i]['ManufactureName'] +
                            '_' +
                            df.iloc[i]['ModelName'] +
                            '____' +
                            df.iloc[j]['ManufactureName'] +
                            '_'+df.iloc[j]['ModelName']
                        ),
                        'Sum_Costs': (
                            df.iloc[i]['AcquisitionPrice'] +
                            df.iloc[j]['AcquisitionPrice']
                        )
                    }, index=[0]), ignore_index=True) 
print(df2) 

数据如下:

fact_date CarID   ManufactureName ModelName    Type   Country AcquisitionPrice
2017-07-14    1   Ford            Focus        Car    USA 11640
2017-07-14    2   Ford            Mustang      Car    USA 12994
2017-07-14    3   Ford            Fiesta       Car    USA 12842
2017-07-14    4   Ford            Mondeo       Car    USA 14685
2017-07-14    1   VW              Jetta        Car    USA 22363
2017-07-14    2   VW              Polo         Car    USA 20107
2017-07-14    3   VW              Golf         Car    USA 21256
2017-07-14    4   VW              Parteon      Car    USA 23679
2017-07-14    1   Toyota          Prius        Car    USA 14384
2017-07-14    2   Toyota          Avensis      Car    USA 14821
2017-07-14    3   Toyota          Corolla      Car    USA 12480
2017-07-14    4   Toyota          Land Cruiser Car    USA 11502
2017-07-14    1   BMW             1er          Car    USA 35127
2017-07-14    2   BMW             2er          Car    USA 43924
2017-07-14    3   BMW             3er          Car    USA 40573
2017-07-14    4   BMW             4er          Car    USA 36690
2017-07-14    1   Mercedes        C-Klasse     Car    USA 36646
2017-07-14    2   Mercedes        A-Klasse     Car    USA 40912
2017-07-14    3   Mercedes        B-Klasse     Car    USA 39060
2017-07-14    4   Mercedes        E-Klasse     Car    USA 41838

0 个答案:

没有答案