Built with Tutify.io

    AIStudyLabs

    Pandas Convert Dictionary to DataFrame

    Pandas Convert Dictionary to DataFrame

    To convert a dictionary into a Pandas DataFrame, you can use the pd.DataFrame() function. The keys of the dictionary will serve as column names or index labels, depending on the structure of the data. The way you convert it depends on the structure of your dictionary.

    In this article, I will explain how to convert a dictionary into a DataFrame using Pandas, emphasizing different methods and techniques depending on the dictionary's structure. I will also discuss essential features, including how to use the orient parameter to define various output formats.

    Key Points –

    <a id="h-convert-a-dictionary-into-a-dataframe"></a>

    Convert a Dictionary Into a DataFrame

    To convert a dictionary into a Pandas DataFrame, you can use the pd.DataFrame() function. Pandas allows you to create a DataFrame from dictionaries with different types of structures, such as dictionaries of lists, dictionaries of dictionaries, etc.

    <a id="h-dictionary-with-lists-as-values"></a>

    Dictionary with Lists as Values

    To create a DataFrame from a dictionary where each key corresponds to a list of values, you can directly use the pd.DataFrame() constructor. Each key will become a column in the DataFrame, and the lists will represent the data for those columns.

    
    import pandas as pd
    
    # Example dictionary
    data = {
        'Courses': ["Spark", "PySpark", "Hadoop", "Pandas"],
        'Fee': [22000, 25000, 24000, 26000],
        'Duration': ['30days', '50days', '40days', '60days'],
        'Discount': [1000, 2300, 2500, 1400]
    }
    
    # Convert to DataFrame
    df = pd.DataFrame(data)
    print("DataFrame from Dictionary with Lists as Values:\n", df)
    

    Here,

    <a id="h-converting-dictionary-to-dataframe-with-orient-index"></a>

    Converting Dictionary to DataFrame With Orient=‘Index’

    Alternatively, to convert a dictionary to a DataFrame using orient='index', you specify that the dictionary keys should be treated as the DataFrame's index labels, and the values should become rows.

    To convert a dictionary where the values are themselves dictionaries, you can use the pd.DataFrame.from_dict() method with the orient='index' parameter. This will treat the outer dictionary's keys as the row indices, and the inner dictionary's keys as the column names.

    
    import pandas as pd
    
    # Example dictionary
    data = {
        'peter': {'Course': 'Spark','Fee': 25000, 'Duration': '30days'},
        'James': {'Course': 'Pyspark', 'Fee': 30000, 'Duration': '40days'},
        'Charlie': {'Course': 'Pandas', 'Fee': 35000, 'Duration': '45days'}
    }
    
    # Convert to DataFrame with keys as index
    df = pd.DataFrame.from_dict(data, orient='index')
    print("DataFrame from Dictionary of Dictionaries (Orient as Index):\n", df)
    

    Here,

    <a id="h-dictionary-with-lists-as-keys-orient-as-index"></a>

    Dictionary with Lists as Keys (Orient as Index)

    When converting a dictionary to a DataFrame where each key is associated with a list, you can orient the DataFrame so that the keys become row indices using the orient='index' parameter.

    
    import pandas as pd
    
    # Example dictionary
    data = {
        'peter': ['Spark', 22000],
        'James': [ 'Pandas', 25000],
        'Charlie': ['Pyspark', 30000]
    }
    
    # Convert to DataFrame with keys as index
    df = pd.DataFrame.from_dict(data, orient='index', columns=['Courses', 'Fee',])
    print("DataFrame from Dictionary with Lists as Keys (Orient as Index):\n", df)
    
    # Output:
    # DataFrame from Dictionary with Lists as Keys (Orient as Index):
    #          Courses    Fee
    # peter      Spark  22000
    # James     Pandas  25000
    # Charlie  Pyspark  30000
    

    Here,

    <a id="h-convert-dictionary-of-series"></a>

    Convert Dictionary of Series

    To convert a dictionary of Pandas Series to a DataFrame, you can directly use the pd.DataFrame() constructor. Each Series will become a column in the DataFrame.

    
    import pandas as pd
    
    # Example dictionary of Series
    data = {
        'Courses': pd.Series(['Spark', 'Pyspark', 'Pandas']),
        'Fee': pd.Series([25000, 30000, 35000]),
        'Duration': pd.Series(['30days', '50days', '40days'])
    }
    
    # Convert to DataFrame
    df = pd.DataFrame(data)
    print("DataFrame from Dictionary of Series:\n", df)
    
    # Output:
    # DataFrame from Dictionary of Series:
    #    Courses    Fee Duration
    # 0    Spark  25000   30days
    # 1  Pyspark  30000   50days
    # 2   Pandas  35000   40days
    

    Here,

    <a id="h-faq-on-pandas-convert-dictionary-to-dataframe"></a>

    FAQ on Pandas Convert Dictionary to DataFrame

    Use pd.DataFrame() directly. The keys will become column names, and the lists will form the data rows.

    Use pd.DataFrame.from_dict(your_dict, orient='index'). This treats the outer keys as index labels, and the inner dictionaries as rows.

    The orient parameter determines how the data is interpreted. 'index' treats the keys as row labels, while 'columns' (default) treats them as column names.

    By passing a columns parameter to pd.DataFrame() or renaming the columns after creation using df.columns.

    Use pd.DataFrame.from_dict(your_dict, orient='index') and customize the DataFrame by splitting or handling tuple keys if needed.

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, converting a dictionary to a DataFrame in Pandas is a flexible and powerful way to organize and analyze data. Depending on the structure of the dictionary. Also learned when we pass the param orient how it can be specified in the output format.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference