My dictionary looks like below. From below dictionary one can see that the key "Column A" contains Multiple values 1,2,3. I would to convert below dictionary to pandas dataframe such that there is duplicate values for Column A. Below is my expected output. I tried using pandas function pd.Dataframe. However, receieved and error that says "ValueError: arrays must all be same length". Please advice how to proceed with dataframe creation.
Thanks in advance for your time!
My Dictionary
my_dict={"Column A":[1,2,3],"ABC":456,"CDE":[12,11]}
Expected Output:
Column A 1
Column A 2
Column A 3
ABC 456
CDE 12
CDE 11
CodePudding user response:
Try:
my_dict = {"Column A": [1, 2, 3], "ABC": 456, "CDE": [12, 11]}
df = pd.DataFrame({"col1": my_dict.keys(), "col2": my_dict.values()})
df = df.explode("col2")
print(df)
Prints:
col1 col2
0 Column A 1
0 Column A 2
0 Column A 3
1 ABC 456
2 CDE 12
2 CDE 11
CodePudding user response:
Try:
code
df = pd.DataFrame(dict([(k,pd.Series(v)) for k,v in my_dict.items()])
).T.stack().reset_index().drop(columns = ['level_1'])
result
level_0 0
0 Column A 1.0
1 Column A 2.0
2 Column A 3.0
3 ABC 456.0
4 CDE 12.0
5 CDE 11.0
CodePudding user response:
Following the question in the comments: In DataFrames and Series, index is always there.
if you want, you can set one of your columns as index, or you can reset it, but it is impossible to remove it. if you want to set col1 as your index, try:
df.set_index('col1')