Yes. It is possible to convert dictionaries into DataFrames
. In fact, a dictionary with strings like keys and values like list is a ready-made form of a pandas DataFrame
. With a dictionary in the cited format, just use the constructor pd.DataFrame
to create the dataframe. Example:
import pandas as pd
data_dict={"Name":["Walter", "Saul", "Hank", "Sjyler"], "Age":[54,56,48,42], "Sex":["M","M","M","F"]}
pd.DataFrame(data_dict)
Output:
Name Age Sex
0 Walter 54 M
1 Saul 56 M
2 Hank 48 M
3 Sjyler 42 F
As you noticed, it is also possible to do this using the combination of constructor list (list()
) with the General zip
. However, in this case, you need to spell out the name of the columns. Example:
names=["Walter", "Saul", "Hank", "Sjyler"]
age=[54,56,48,42]
sex=["M","M","M","F"]
pd.DataFrame(list(zip(names,age,sex)), columns=["Name", "Age", "Sex"])
Ouput:
Name Age Sex
0 Walter 54 M
1 Saul 56 M
2 Hank 48 M
3 Sjyler 42 F
If you do not explain the column name, the command also works, but the column name will be the default: [1,2,3]
.
Additional information about the function zip
. This function applies a kind of distributive in the lists to create a series tuples. It belongs to the class of generators
which are objects used for iteration, but which, unlike other objects used for iteration, such as list comprehensions, do not generate a result. By not producing output, these objects use less memory. In your specific case, you only saw the result of the function zip
why did you use the list
constructor.
All this to say that, although it is possible, the construction of dataframes from zip functions is perhaps not the most common procedure. Using dictionaries seems more natural and simpler.
Only one detail: according to documentation,
zip
does not return a dictionary, but rather an iterator of tuples. So if you dozip(A, B)
, it returns an iterator which, at each iteration, returns a tuple containing an element ofA
and another ofB
. In doinglist(zip(A, B))
, you have a list of all these tuples (see here an example). What pandas does is use these tuples to create the dataframe, and doesn’t even need tolist
, can use onlyzip
straightforward, see– hkotsubo