簡體   English   中英

Pandas-根據開關用數據框填充字典

[英]Pandas- Fill a dictionary with dataframes depending on a switch

背景:我有一些數據幀可以通過開關打開或關閉。 我想用每個打開的數據框填充字典。 然后我希望能夠遍歷 dataframe。

問題:我不知道如何動態構建我的字典以僅在打開開關時包含數據幀。

我試過的:

import pandas as pd

sw_a = True
sw_b = False
sw_c = True

a = pd.DataFrame({'IDs':[1234,5346,1234,8793,8793],
                   'Cost':[1.1,1.2,1.3,1.4,1.5],
                    'Names':['APPLE','Orange','STRAWBERRY','Grape','Blue']}) if sw_a == True else []
b = pd.DataFrame({'IDs':[1,2],
                   'Cost':[1.1,1.2],
                    'Names':['APPLE1','Blue1']}) if sw_b == True else []
c = pd.DataFrame({'IDs':[12],
                  'Cost':[1.5],
                    'Names':['APPLE2']}) if sw_c == True else []
total = {"first":a,"second":b,"third":c}

for df in total:
    temp_cost = sum(total[df]['Cost'])
    print(f'The number of fruits for {df} is {len(total[df])} and the cost is {temp_cost}')

以上不起作用,因為它始終包含數據幀,如果開關關閉,它是一個字符串而不是完全排除。

考慮這樣的事情。

sw_a = True
sw_b = False
sw_c = True

a = pd.DataFrame({'IDs':[1234,5346,1234,8793,8793],
                   'Cost':[1.1,1.2,1.3,1.4,1.5],
                    'Names':['APPLE','Orange','STRAWBERRY','Grape','Blue']})
b = pd.DataFrame({'IDs':[1,2],
                   'Cost':[1.1,1.2],
                    'Names':['APPLE1','Blue1']})
c = pd.DataFrame({'IDs':[12],
                  'Cost':[1.5],
                    'Names':['APPLE2']})

total = {}
if sw_a == True:
    total['sw_a'] = a
if sw_b == True:
    total['sw_b'] = b
if sw_c == True:
    total['sw_c'] = c
print(total)

for df in total:
    temp_cost = sum(total[df]['Cost'])
    print(f'The number of fruits for {df} is {len(total[df])} and the cost is {temp_cost}')

The number of fruits for sw_a is 5 and the cost is 6.5
The number of fruits for sw_c is 1 and the cost is 1.5

我的設置與您的類似,但我不關心每個 dataframe 分配上的開關:

import pandas as pd

sw_a = True

sw_b = False
sw_c = True

a = pd.DataFrame({'IDs':[1234,5346,1234,8793,8793],
                   'Cost':[1.1,1.2,1.3,1.4,1.5],
                    'Names':['APPLE','Orange','STRAWBERRY','Grape','Blue']})
b = pd.DataFrame({'IDs':[1,2],
                   'Cost':[1.1,1.2],
                    'Names':['APPLE1','Blue1']})
c = pd.DataFrame({'IDs':[12],
                  'Cost':[1.5],
                    'Names':['APPLE2']})

total = {"first":a,"second":b,"third":c} # don't worry about the switches yet.

我們現在才過濾:

list_switches = [sw_a, sw_b, sw_c] # the switches! finally!
total_filtered = {tup[1]:total[tup[1]] for tup in zip(list_switches, total) if tup[0]}

並像你所做的那樣繼續。

for df in total_filtered:
    temp_cost = sum(total[df]['Cost'])
    print(f'The number of fruits for {df} is {len(total[df])} and the cost is {temp_cost}')

Output:

在此處輸入圖像描述

編輯您可以稍微zip功能,例如,如果您正在構建數據幀列表、dataframe 名稱和動態切換,並且可以確保它們始終具有相同的長度,您可以執行以下操作:

# pretend these three lists are coming from somewhere else and can have variable length, rather than being hard-coded.
list_dfs = [a,b,c]
list_switches = [sw_a, sw_b, sw_c]
list_names = ["first", "second", "third"]

# use a zip object over the three lists.
zipped = zip(list_dfs, list_switches, list_names)
total = {tup[2] : tup[0] for tup in zipped if tup[1]}

for df in total:
    temp_cost = sum(total[df]['Cost'])
    print(f'The number of fruits for {df} is {len(total[df])} and the cost is {temp_cost}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM