Day-3 Data Mastery: Learn to Create pandas Data Frame (Easy Tutorial!)

Akansha Yadav shows six ways to create a pandas DataFrame in Python: from a dictionary, list of lists, NumPy array, Series, list of dictionaries, and dictionary of Series. Each method includes code examples and output, plus five practice questions to test what you learned.
Chapters
Ladies and gentlemen, hello everyone, welcome back to my YouTube channel. In today's video, we will be talking all about DataFrame in Pandas. So in Pandas, there is a very important concept of DataFrame. Pandas has basically two data structures. One is the Series, another one is the DataFrame. In the last video, we talked about Series. Now in this video, we are going to talk about DataFrame, how to create DataFrame, what are the different ways using which we can create DataFrame. So let's get back on the screen and see how to create it. So in the last video, as I told you, DataFrame is just like a spreadsheet. So in spreadsheet, we have rows and columns. So let's see how we can create data frames. What are the different ways using which we can create? So the very first way is using a dictionary. So using dictionary also you can create it. Second is using list of list. Third is using a NumPy array. The number fourth is using, uh, series as well. You can create a data frame. And the fifth number is list of dictionaries. So that is also a part of one-dimensional. So we will be creating dictionaries. Another is the dictionary of series. So let's import pandas in our file. So once I have imported, I have to create a DataFrame. So first we will see that how to create DataFrame using a dictionary. So dictionary is a one-dimensional data, but let's see how to create a 2D dimensional data, 2D data basically. So let's create a data. So I'm creating column 1 where I am storing 1, 2, 3, 4, 5 values. In column 2, I am going to store some string values like num, num2, and something similar to that. I am adding, adding 5 values. Because I'm looking forward to create 5 rows. 2 columns are there, column 1 and column 2, and I have 5 rows. So let's see. So for creating DataFrame, we have to use pd.DataFrame, and then in the brackets we have to write down the data that we have. Now I'm going to print to check whether this is created. So yes, we have 2 columns and 5 rows in the output. If you wanted to add on any column, you can similarly create one more key-value pair in the same dictionary. The second way is using a list of lists. So list is a one-dimensional data. So let's see how to use multiple lists to create a two-dimensional data. So first is we have to create a list data. So I have created one list, and within that list I am going to create multiple lists. So first list is 1, A, 56, which is— which means that there are 3 values. Now next is 2, B, 65, 3, V, 85. So basically what I have done is I have 3 in total and 3, 3 into 3, that is the 2D data of 3 into 3 matrix. pd.DataFrame, now in the brackets I'm assigning the data to data list which I have created. Now what you can do is you have to write down the column. So whatever name you wanted to give to the column, you can write it. As a sample, I am just writing column 1, column 2, and column 3 because I have 3 values. It means 3 rows and 3 columns. So now let's print the list_df. So here we go with the list_df. We have 3 columns and 3 rows. Now next way is that using our NumPy array. So NumPy is an important library if you are working with the pandas. So I've imported the NumPy as np. Now I have to create an array. To create a NumPy array, I am going to use np.array method, and then I have to write down what type of array I wanted to create. I wanted to create a numeric array, but this is gonna be multiple values. So first is I have written 1, 2, 3, 4, 5. Next I am writing only for 4 values I am taking. Then I'm taking 5, 6, 7, 8. So this is my data which I have created using NumPy. Now I have to create the DataFrame. So pd.DataFrame method, as I told you at the first step. So now we have to assign data is equal to the NumPy data which we have created. You have to assign the columns as well because we have not defined any column. So you have to write down the column within the form of a list. Make sure you are writing it in the list. Column 1, column 2, column 3, and column 4 because we have 4 columns and 2 rows in this. The number of list— the number of arrays that you have created will be the rows, and the number of values which you are writing are the columns. You can remove it and you can replace the column names with something else. Now, next way is using a series. So series is one-dimensional data. If you are going to create multiple series and going to use those series as a column in the DataFrame, then you can easily create one by series. So let's create a series, simple series. I am creating with a numeric number. S2 series is also gonna be some similar to that, but this is going to be other string one. So I'm writing math, science, English, social, and then I'm writing English, and next is the Python, because we have selected 5 values in this one. So now let's create pd.DataFrame, and within that you have to write column 1. Now this is, uh, something tricky, you can say, that within the dictionary format you have to write it. So in the curly brackets, I am writing column is equal to 1, s1. Column 2 is equal— is within the column S2. So basically 2 series I am using, so I have to use the dictionary format to do the same. So we have 2 columns over here and multiple values. The last way I would say is the from a list of dictionaries. So it means I have to create a list. It means the major thing will be the list, and within that list there will be the dictionaries. So within that list, it means I'm using the square bracket, which means it's list. Now in the curly brackets, I am writing some values, key-value pairs, which denotes that this is the dictionary. So this is column 1, 23, column 2, 35. Same way I'm going to copy it and paste it in the next line and going to change some values because column 1 and column 2 are going to be the same throughout my dictionary because I want 2 columns. If you want more than within the first one, you have to change it, you have to basically update it with more values. Now I'm going to create a DataFrame So pd.DataFrame, I will be using the dictionary list. Okay, pd.DataFrame. Now in the brackets I have to write down data is equal to list d. Print. Now I have to print. So now we have 2 columns and 3 rows as I have created 3 dictionaries within the list. The last way to create a dictionary is using a dictionary of a series. That we did in one, one more step. I will show you how to do it. The sixth one is using a dictionary of series. So, uh, from a series we did is this. So I'm copying pasting the same code. This is S1, this is S2, and within the dictionary, within the creation of the DataFrame, I have using the dictionary to create it. So these are the 5 questions which I recommend you to practice if you wanted to excel in this field of DataFrame and Pandas. These questions are very, very realistic questions, real world questions I have selected for you. So you have to go and practice these questions. You can drop your solutions on my LinkedIn profile. And also, if you need any help, you can comment down below this video. We'll meet in the next video. Till then, bye-bye.










