[Python] Utilizing a loop or automated method instead of manually typing out each dataframe...

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    Summary: I want to be able to create list logic that can read any dataframe (varying column headers, data types etc) and assign it into the following structure list

    [{'Name': column header 1 name, 'Type': column header 1 type, 'Name': column header 2 name, 'Type': column header 2 name}]


    I have a sample table df_stack_exchange

    data_stack_exchange = {'store': ['A','B', 'B', 'C', 'C', 'C', 'D', 'D', 'D', 'D'],
    'worker': [1,1,2,1,2,3,1,2,3,4],
    'boxes': [105, 90, 100, 80, 10, 200, 70, 210, 50, 0]}
    df_stack_exchange = pandas.DataFrame(data_stack_exchange)


    And want to create the following. I know it is not a functional dictionary with the duplicated keys but its for another defined purpose that requires this rigid structure

    [{'Name': 'store', 'Type': 'object', 'Name': 'worker', 'Type': 'int64', 'Name': 'boxes', 'Type': 'int64'}]

    Continue reading...

Compartilhe esta Página