Convert list of dictionaries to a pandas DataFrame

Convert list of dictionaries to a pandas DataFrame

To convert a list of dictionaries into a Pandas DataFrame, you can use the pd.DataFrame() constructor. Each dictionary in the list represents a row in the DataFrame, where the keys in the dictionaries become column names. Here's how you can do it:

import pandas as pd # Example list of dictionaries data = [ {"Name": "Alice", "Age": 25, "City": "New York"}, {"Name": "Bob", "Age": 30, "City": "San Francisco"}, {"Name": "Charlie", "Age": 22, "City": "Los Angeles"}, ] # Convert the list of dictionaries to a DataFrame df = pd.DataFrame(data) # Display the resulting DataFrame print(df) 

This will create a DataFrame where each dictionary in the data list corresponds to a row, and the keys ("Name," "Age," "City") become column names.

Output:

 Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 22 Los Angeles 

You can also specify the order of columns by passing a list of column names as the columns parameter to the pd.DataFrame() constructor if you want them to appear in a specific order:

import pandas as pd # Example list of dictionaries data = [ {"Name": "Alice", "Age": 25, "City": "New York"}, {"Name": "Bob", "Age": 30, "City": "San Francisco"}, {"Name": "Charlie", "Age": 22, "City": "Los Angeles"}, ] # Define the desired column order columns = ["Name", "City", "Age"] # Convert the list of dictionaries to a DataFrame with custom column order df = pd.DataFrame(data, columns=columns) # Display the resulting DataFrame print(df) 

This will create a DataFrame with the specified column order.

Output:

 Name City Age 0 Alice New York 25 1 Bob San Francisco 30 2 Charlie Los Angeles 22 

In this way, you can easily convert a list of dictionaries into a Pandas DataFrame with the desired structure.

Examples

  1. Query: "Convert list of dictionaries to pandas DataFrame in Python"

    Description: Users often want to convert a list of dictionaries into a pandas DataFrame for further data analysis or manipulation.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': 28, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Convert to DataFrame df = pd.DataFrame(data) print(df) 
  2. Query: "Python code to convert list of dicts to DataFrame"

    Description: Users search for Python code snippets to convert a list of dictionaries to a pandas DataFrame efficiently.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': 28, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Convert to DataFrame df = pd.DataFrame.from_records(data) print(df) 
  3. Query: "Python pandas DataFrame from list of dictionaries"

    Description: Users are interested in creating a pandas DataFrame from a list of dictionaries in Python.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': 28, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Convert to DataFrame df = pd.DataFrame(data) print(df) 
  4. Query: "Convert list of dictionaries to pandas DataFrame with custom columns"

    Description: Users want to convert a list of dictionaries into a pandas DataFrame while specifying custom column names.

    import pandas as pd # List of dictionaries data = [ {'name': 'John', 'age': 30, 'city': 'New York'}, {'name': 'Emma', 'age': 28, 'city': 'Los Angeles'}, {'name': 'Mike', 'age': 35, 'city': 'Chicago'} ] # Custom column names columns = ['Name', 'Age', 'City'] # Convert to DataFrame df = pd.DataFrame(data, columns=columns) print(df) 
  5. Query: "Convert list of dicts to DataFrame with specified columns in Python"

    Description: Users seek a way to convert a list of dictionaries to a pandas DataFrame while specifying specific columns.

    import pandas as pd # List of dictionaries data = [ {'name': 'John', 'age': 30}, {'name': 'Emma', 'age': 28}, {'name': 'Mike', 'age': 35} ] # Specified columns columns = ['Name', 'Age'] # Convert to DataFrame df = pd.DataFrame(data, columns=columns) print(df) 
  6. Query: "Python pandas DataFrame from list of dictionaries with missing values"

    Description: Users want to create a pandas DataFrame from a list of dictionaries, handling missing values gracefully.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': None, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Convert to DataFrame df = pd.DataFrame(data) print(df) 
  7. Query: "Convert list of dictionaries to pandas DataFrame with index"

    Description: Users want to convert a list of dictionaries into a pandas DataFrame while specifying an index.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': 28, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Specified index index = ['A', 'B', 'C'] # Convert to DataFrame df = pd.DataFrame(data, index=index) print(df) 
  8. Query: "Python pandas DataFrame from list of dictionaries with nested data"

    Description: Users want to convert a list of dictionaries containing nested data structures into a pandas DataFrame.

    import pandas as pd # List of dictionaries with nested data data = [ {'Name': 'John', 'Info': {'Age': 30, 'City': 'New York'}}, {'Name': 'Emma', 'Info': {'Age': 28, 'City': 'Los Angeles'}}, {'Name': 'Mike', 'Info': {'Age': 35, 'City': 'Chicago'}} ] # Convert to DataFrame df = pd.json_normalize(data) print(df) 
  9. Query: "Python code to convert list of dictionaries to DataFrame with selected keys"

    Description: Users want to convert a list of dictionaries into a pandas DataFrame while selecting specific keys for columns.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': 30, 'City': 'New York'}, {'Name': 'Emma', 'Age': 28, 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': 35, 'City': 'Chicago'} ] # Selected keys for columns keys = ['Name', 'City'] # Convert to DataFrame df = pd.DataFrame(data)[keys] print(df) 
  10. Query: "Convert list of dictionaries to pandas DataFrame with data type conversion"

    Description: Users want to convert a list of dictionaries into a pandas DataFrame while converting data types of certain columns.

    import pandas as pd # List of dictionaries data = [ {'Name': 'John', 'Age': '30', 'City': 'New York'}, {'Name': 'Emma', 'Age': '28', 'City': 'Los Angeles'}, {'Name': 'Mike', 'Age': '35', 'City': 'Chicago'} ] # Convert to DataFrame with data type conversion df = pd.DataFrame(data) df['Age'] = df['Age'].astype(int) # Convert 'Age' column to integer print(df) 

More Tags

accordion http-status-code-410 android-relativelayout dd skyscanner aar ios azure-pipelines-release-pipeline cucumber-java homekit

More Python Questions

More Weather Calculators

More Everyday Utility Calculators

More Geometry Calculators

More Fitness-Health Calculators