Pandas-เติมเต็มเป็นพจนานุกรมกับ dataframes ขึ้นอยู่กับสวิทช์

0

คำถาม

พื้นหลังของฉันมีไม่กี่ dataframes นั่นอาจจะเปิดหรือปิดกับ switches. ฉันต้องการเพื่อเติมเต็มเป็นพจนานุกรมกับแต่ละคนเปลี่ยนต่อ dataframes. งั้นฉันต้องการที่จะสามารถที่จะเล่นบ dataframe.

ปัญหาฉันไม่รู้ว่าจะ dynamically สร้างพจนานุกรมของฉันจะแค่รวม dataframes ตอนที่พวกเขา switches เปลี่ยนแปลง

สิ่งที่ฉันพยายาม:

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}')

ที่อยู่เหนือมันไม่ได้ผลเพราะมันเสมอถึง dataframes ถ้าเปลี่ยนคือปิดมันเป็นข้อความแทนที่จะเป็นขนม.

dataframe dictionary for-loop pandas
2021-11-23 19:01:19
2

คำตอบที่ดีที่สุด

1

ฉันจัดฉาก-นี่คล้ายกับของคุณแต่ฉันไม่รบกวกับ switches อยู่กั 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}')

แสดงผล:

enter image description here

แก้ไข คุณสามารถเป็นนิดหน่อ fancier กับ zip functionlity ตัวอย่างเช่นถ้าคุณ constructing ในรายชื่อของ dataframes,dataframe ชื่อและ switches dynamically และสามารถมั่นใจว่าพวกมันจะได้เหมือนเดิมเสมอไปหรอกความยาว,คุณสามารถ\ทำอะไรบางอย่างเหมือน:

# 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}')
2021-11-23 19:47:38

มันทำงานผ่านมาได้อย่างสวยงามแต่ฉันไม่แน่ใจว่ามีบรรทัดนี้ทำงาน...ทั้งหมด={tup[2]:tup[0]สำหรับ tup ใน zipped ถ้า tup[1]}
Jonathan Hay

@JonathanHay--มันเป็น dict รู้จักเลตรองแสเดาใจ พวกคุณคุ้นๆกับเหมือนกัของพวกนี้ทบทวนคอนเซ็ปท์? ขอบคุณสำหรับ upvote และยอมรับ,btw.
butterflyknife
1

พิจารณาเรื่องแบบนี้

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
2021-11-23 19:32:55

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่