'Creating dataframe from a dictionary where value is an array [closed]
I am new to Python world. How can we create a dataframe with an existing dictionary where the values are arrays.
The data looks like :
data={'first':['A','B','C','D','E','F'],'second':[10,20,30,40,50,60],'third':[1.1,2.5,3.4,5.4,6.7,8.9]}
After the. creation of the dataframe, it would look like this :
first second third
A 10 1.1
B 20 2.5
C 30 3.4
D 40 5.4
. . .
. . .
Solution 1:[1]
Do it simply using pd.DataFrame() OR pd.DataFrame.from_dict()
import pandas as pd
data=pd.DataFrame({'first':['A','B','C','D','E','F'],'second':[10,20,30,40,50,60],'third':[1.1,2.5,3.4,5.4,6.7,8.9]})
print(data)
OR
import pandas as pd
data={'first':['A','B','C','D','E','F'],'second':[10,20,30,40,50,60],'third':[1.1,2.5,3.4,5.4,6.7,8.9]}
data = pd.DataFrame.from_dict(data)
print(data)
Solution 2:[2]
I don't have the full experience in Python Dataframe, but I think you get the result in this manner via this helping link: Python Pandas Dataframe
# import pandas as pd
import pandas as pd
# Dictionary with list values
data={'first':['A','B','C','D','E','F'],'second':[10,20,30,40,50,60],'third':[1.1,2.5,3.4,5.4,6.7,8.9]}
# Calling DataFrame constructor on Dictionary
data_frame = pd.DataFrame(data)
print(data_frame)
Solution 3:[3]
import pandas as pd
data={'first':['A','B','C','D','E','F'],'second':[10,20,30,40,50,60],'third':[1.1,2.5,3.4,5.4,6.7,8.9]}
df = pd.DataFrame(data)
printf(df)
Solution 4:[4]
import pandas as pd
data={'first':['A','B','C','D','E','F'],
'second':[10,20,30,40,50,60],
'third':[1.1,2.5,3.4,5.4,6.7,8.9]}
df = pd.DataFrame(data) #Creating Data Frame
print(df) #Printing The Data Frame
print(type(df)) #Checking The Type
More: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | code_till_u_die |

