How to create Pandas DataFrame

Geomario
2 min readOct 22, 2020

--

A Pandas DataFrame is the concatenation of a group of Pandas Series. That's how to create PandasDataFrame, simple.

Pandas DataFrame?

Let´s code!

#Code block 1.0#Data    #(1)data_foo  = ["one", "one", "one", "two", "two", "two"]
data_bar = ["A", "B", "C", "A", "B", "C"]
data_baz = [1, 2, 3, 4, 5, 6]
data_zoo = ["x", "y", "z", "q", "w", "t"]
index = [0, 1, 2, 3, 4, 5] #(2)

In the code block 1.0, we have created four lists (1) with the information that will be contained in our columns, the columns foo, bar, baz and zoo. The index list (2), holds the length of the Series, in this case, the index from 0 to 5, which corresponds to 6 values.

#Code Block 2.0#(1)
s_f = pd.Series(data_foo, index=index)
s_bar = pd.Series(data_bar, index=index)
s_baz = pd.Series(data_baz, index=index)
s_zoo = pd.Series(data_zoo, index=index)
#(2)
d_df = {"foo" : s_f,
"bar" : s_bar,
"s_baz" : s_baz,
"zoo" : s_zoo}

In the code block 2.0, we have created our Pandas Series, Remember that a Pandas DataFrame is the concatenation of Pandas Series! After the Pandas Series creation (1), we must allocate the Series creation into a Dictionary (2).

#Code block 3.0#Create Data Frame
df = pd.DataFrame(d_df) #(1)
print (df)

The last step is to create the DataFrame from the dictionary that contains the Pandas Series (1). There you go, you got a Pandas DataFrame from a dictionary. There are other methods; we can talk about them in later posts.

Clarify your questions with my video from my youtube channel. ⬇️⬇️

Do follow me and give me a clap, please? 👏👏👏

Thanks & Happy coding!

--

--

Geomario
Geomario

Written by Geomario

👨‍💻 Software & Data Developer | Software Research Engineer | MLE

No responses yet