Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套循环数据帧python_Python_Loops_Dataframe_Nested Loops - Fatal编程技术网

嵌套循环数据帧python

嵌套循环数据帧python,python,loops,dataframe,nested-loops,Python,Loops,Dataframe,Nested Loops,我想使用1W、国家和汽车0的值计算总成本,并将其添加到1M汽车0中,对汽车1、2、…、10执行相同操作。然后对1W和3M等进行同样的操作。我在代码中使用了嵌套循环,如下所示,但这太长了。是否有可能直接使用熊猫而不是执行嵌套循环 组合应如下所示: 我的CSV文件如下所示: 我预期的结果如下: 您是否询问是否可以基于列值有条件地筛选数据帧?例如:df[df['Rental_Duration'].isin([1W','3M'])这并不能直接回答您的问题,但根据我的理解,您可能需要将Rental_Dur

我想使用1W、国家和汽车0的值计算总成本,并将其添加到1M汽车0中,对汽车1、2、…、10执行相同操作。然后对1W和3M等进行同样的操作。我在代码中使用了嵌套循环,如下所示,但这太长了。是否有可能直接使用熊猫而不是执行嵌套循环

组合应如下所示:

我的CSV文件如下所示:

我预期的结果如下:


您是否询问是否可以基于列值有条件地筛选数据帧?例如:
df[df['Rental_Duration'].isin([1W','3M'])
这并不能直接回答您的问题,但根据我的理解,您可能需要将
Rental_Duration
更改为
Rental_Start
,并在fly@rahlf23,不,我想要更多的东西,以获得租赁期1W和1M等的总成本,然后将其存储起来。更清楚吗?@JeffXiao,不,我想知道更多关于租赁期1W和1M等成本的总和,然后储存起来。更清楚吗?
USD:
1W and 1M
1W and 3M
1W and 6M
1W and 1Y

and
1M and 3M
1M and 6M
1M and 1Y

and
3M and 6M
3M and 1Y

and
6M and 1Y
import pandas as pd

csv_loader = pd.read_csv('Cars.csv', encoding='cp1252', sep=';', index_col=0).dropna()

for current_date in csv_loader.index.unique():
    print('calculating date: ' +str(current_date))
    for index, row in index_data.iterrows():
        if index == current_date:

            for index2, row2 in index_data.iterrows():
                if index2 == current_date:
fact_date;Cars;Costs;Country;Car;Rental_Duration
2017-07-14;0;157.00;USD;Ford;1W
2017-07-14;1;122.00;USD;Ford;1W
2017-07-14;2;122.00;USD;Ford;1W
2017-07-14;3;102.00;USD;Ford;1W
2017-07-14;4;153.00;USD;Ford;1W
2017-07-14;5;130.00;USD;Ford;1W
2017-07-14;6;151.00;USD;Ford;1W
2017-07-14;7;132.00;USD;Ford;1W
2017-07-14;8;153.00;USD;Ford;1W
2017-07-14;9;191.00;USD;Ford;1W
2017-07-14;10;131.00;USD;Ford;1W
2017-07-14;0;571.00;USD;Ford;1M
2017-07-14;1;526.00;USD;Ford;1M
2017-07-14;2;568.00;USD;Ford;1M
2017-07-14;3;546.00;USD;Ford;1M
2017-07-14;4;593.00;USD;Ford;1M
2017-07-14;5;541.00;USD;Ford;1M
2017-07-14;6;536.00;USD;Ford;1M
2017-07-14;7;532.00;USD;Ford;1M
2017-07-14;8;537.00;USD;Ford;1M
2017-07-14;9;582.00;USD;Ford;1M
2017-07-14;10;523.00;USD;Ford;1M
2017-07-14;0;679.00;USD;Ford;3M
2017-07-14;1;641.00;USD;Ford;3M
2017-07-14;2;622.00;USD;Ford;3M
2017-07-14;3;654.00;USD;Ford;3M
2017-07-14;4;655.00;USD;Ford;3M
2017-07-14;5;635.00;USD;Ford;3M
2017-07-14;6;623.00;USD;Ford;3M
2017-07-14;7;678.00;USD;Ford;3M
2017-07-14;8;689.00;USD;Ford;3M
2017-07-14;9;694.00;USD;Ford;3M
2017-07-14;10;625.00;USD;Ford;3M
2017-07-14;0;726.00;USD;Ford;6M
2017-07-14;1;718.00;USD;Ford;6M
2017-07-14;2;770.00;USD;Ford;6M
2017-07-14;3;793.00;USD;Ford;6M
2017-07-14;4;704.00;USD;Ford;6M
2017-07-14;5;739.00;USD;Ford;6M
2017-07-14;6;785.00;USD;Ford;6M
2017-07-14;7;701.00;USD;Ford;6M
2017-07-14;8;718.00;USD;Ford;6M
2017-07-14;9;714.00;USD;Ford;6M
2017-07-14;10;788.00;USD;Ford;6M
2017-07-14;0;992.00;USD;Ford;1Y
2017-07-14;1;935.00;USD;Ford;1Y
2017-07-14;2;981.00;USD;Ford;1Y
2017-07-14;3;955.00;USD;Ford;1Y
2017-07-14;4;920.00;USD;Ford;1Y
2017-07-14;5;978.00;USD;Ford;1Y
2017-07-14;6;943.00;USD;Ford;1Y
2017-07-14;7;911.00;USD;Ford;1Y
2017-07-14;8;969.00;USD;Ford;1Y
2017-07-14;9;922.00;USD;Ford;1Y
2017-07-14;10;978.00;USD;Ford;1Y
fact_date Cars    Costs_Total Car Country Rental_Duration
2017-07-14    0   728 Ford    USD 1W_1M
2017-07-14    1   648 Ford    USD 1W_1M
2017-07-14    2   690 Ford    USD 1W_1M
2017-07-14    3   648 Ford    USD 1W_1M
2017-07-14    4   746 Ford    USD 1W_1M
2017-07-14    5   671 Ford    USD 1W_1M
2017-07-14    6   687 Ford    USD 1W_1M
2017-07-14    7   664 Ford    USD 1W_1M
2017-07-14    8   690 Ford    USD 1W_1M
2017-07-14    9   773 Ford    USD 1W_1M
2017-07-14    10  654 Ford    USD 1W_1M
2017-07-14    0   836 Ford    USD 1W_3M
2017-07-14    1   763 Ford    USD 1W_3M
2017-07-14    2   744 Ford    USD 1W_3M
2017-07-14    3   756 Ford    USD 1W_3M
2017-07-14    4   808 Ford    USD 1W_3M
2017-07-14    5   765 Ford    USD 1W_3M
2017-07-14    6   774 Ford    USD 1W_3M
2017-07-14    7   810 Ford    USD 1W_3M
2017-07-14    8   842 Ford    USD 1W_3M
2017-07-14    9   885 Ford    USD 1W_3M
2017-07-14    10  756 Ford    USD 1W_3M
2017-07-14    0   883 Ford    USD 1W_6M
2017-07-14    1   840 Ford    USD 1W_6M
2017-07-14    2   892 Ford    USD 1W_6M
2017-07-14    3   895 Ford    USD 1W_6M
2017-07-14    4   857 Ford    USD 1W_6M
2017-07-14    5   869.00  Ford    USD 1W_6M
2017-07-14    6   936.00  Ford    USD 1W_6M
2017-07-14    7   833.00  Ford    USD 1W_6M
2017-07-14    8   871.00  Ford    USD 1W_6M
2017-07-14    9   905.00  Ford    USD 1W_6M
2017-07-14    10  919.00  Ford    USD 1W_6M