DataFrame键值对列拆分转置:将键设为表头的技术问询
Got it, let's walk through how to split that dictionary-filled column into proper table columns—this is a super common task in pandas, so I've got you covered.
Scenario Recap
You have a DataFrame where one column (let's say it's named Data) contains dictionaries. Each dictionary has ~50 key-value pairs, and you want those keys to become new column headers, with their corresponding values as row entries. For example, your original DataFrame might look like this:
import pandas as pd # Sample original DataFrame df_Normal = pd.DataFrame({ "Data": [ {"ProductID": "P001", "Price": 29.99, "Category": "Electronics"}, {"ProductID": "P002", "Price": 14.99, "Category": "Apparel"} ] })
Solution 1: Use pd.json_normalize() (Most Efficient)
The cleanest and fastest way to handle this is with pandas' built-in json_normalize() function. It's purpose-built for flattening JSON-like structures (like dictionaries) into tabular data, and it's way more efficient than manual methods for large datasets.
Code Implementation:
# Extract the list of dictionaries from the 'Data' column dict_records = df_Normal["Data"].tolist() # Normalize the dictionaries into a new DataFrame normalized_df = pd.json_normalize(dict_records) # If you need to keep other columns from the original DataFrame, join them back # Example: if original df has an 'OrderID' column: # normalized_df = df_Normal.drop("Data", axis=1).join(normalized_df)
Result:
| ProductID | Price | Category |
|---|---|---|
| P001 | 29.99 | Electronics |
| P002 | 14.99 | Apparel |
Solution 2: Use apply(pd.Series) (For Smaller Datasets)
If you're working with a small dataset, you can also use apply() to convert each dictionary into a pandas Series. This will automatically align dictionary keys as column headers. Note that this method is slower for large datasets, but works perfectly for smaller ones.
Code Implementation:
normalized_df = df_Normal["Data"].apply(pd.Series) # Join back original non-'Data' columns if needed: # normalized_df = df_Normal.drop("Data", axis=1).join(normalized_df)
Key Notes
- Both methods handle missing keys gracefully: if a dictionary doesn't include a key present in others, the corresponding cell will be filled with
NaN. - If your dictionaries have nested structures (e.g.,
{"Product": {"ID": "P001"}, "Seller": {"Name": "TechStore"}}),json_normalize()can handle that too with parameters likerecord_pathandmetato flatten nested levels.
内容的提问来源于stack exchange,提问作者Peter Lucas

