試圖了解如何在 python 中使用嵌套循環(huán)。我試圖理解總結(jié)相同的值并學(xué)會(huì)了使用 group_by 函數(shù)(基于我今天看到的 stackoverflow 中的另一個(gè)問題)。我想學(xué)習(xí) pytonic-dataframe 方式。現(xiàn)在我想用以下方式總結(jié)工作日。我根據(jù)場(chǎng)景總結(jié)單位,例如:Scenario = 1,Company = A,Country = USA,Unit = HR+Corporate Client,總結(jié)工作時(shí)間 = 65+63 = 128 等等。在原始數(shù)據(jù)之后我包括輸出應(yīng)該是什么樣子。我不確定這是否也適用于 group_by,這更像是一種樞軸方式。我從嵌套循環(huán)開始,但在索引日期時(shí)遇到問題。因此,我的代碼僅按日期過濾,效率不高,但有效。我了解到嵌套循環(huán)對(duì)于數(shù)據(jù)幀是不夠的,但不確定我可以走哪條路。代碼如下所示:import pandas as pdworking_date_start = '2017-07-14'working_date_end = '2017-07-15'flag_scenario = 0 Scenario = 0df = pd.read_csv('C:/Comapny_WorkingHours.csv', encoding='cp1252', sep=';', index_col=None).dropna()df = df[(df['working_date'] >= working_date_start) & (df['working_date'] < working_date_end) & (df['flag'] == flag_scenario) & (df['Scenario'] >= Scenario)]pd_date = pd.DatetimeIndex(df['working_date'].values)df['working_date'] = pd_dateindex_data = df.set_index('working_date')for current_date in index_data.index.unique(): print('calculating date: ' +str(current_date)) for i in range(0, len(df)): for j in range(i+1, len(df)): if df.iloc[i]['Scenario'] == df.iloc[j]['Scenario'] and df.iloc[i]['Unit'] != df.iloc[j]['Unit'] and df.iloc[i]['Company'] == 'Company A' and df.iloc[j]['Company'] == 'Company A' and df.iloc[i]['Country'] == 'USA' and df.iloc[j]['Country'] == 'USA': print(df.iloc[i]['Scenario'], df.iloc[j]['Scenario']) print(df.iloc[i]['Unit'], df.iloc[j]['Unit'])
Python 中的數(shù)據(jù)分析(Dataframe 和嵌套循環(huán))
繁華開滿天機(jī)
2021-06-03 01:13:39