gpt-4.1-mini-2025-04-14· openai
from WildChat-4.8M
i have a dictionary which has list related to keys. for example: dict{key1:[a,b,c],…
- User
i have a dictionary which has list related to keys. for example: dict{key1:[a,b,c], key2:[d,e,f]} i want to write them a dataframe such that 1 key is related to one row and the element in list mist be separated by \n
- Assistant
You can create a pandas DataFrame from a dictionary where each key corresponds to one row, and the list values are converted into a single string with elements separated by
\n.Here is a sample code to do that:
pythonimport pandas as pd # Your dictionary data = { 'key1': ['a', 'b', 'c'], 'key2': ['d', 'e', 'f'] } # Convert dictionary to DataFrame df = pd.DataFrame({ 'Key': list(data.keys()), 'Values': ['\n'.join(v) for v in data.values()] }) print(df)Output:
codeKey Values 0 key1 a\nb\nc 1 key2 d\ne\nfThis creates a DataFrame where each row corresponds to one key, and the list elements are joined with newline characters inside the 'Values' column.