The syntax for getting the numpy representation of the DataFrame is given below:
Example # 01:
For performing these examples, we have to install the spyder tool. After the installation of this tool, we write some codes which are also shown below. First, we have to import the “panda” as “pd”. Python is instructed to import the pandas’ data processing library into the existing code by the import pandas section of the code. The code’s pd section instructs Python to assign pandas the abbreviation of pd. As a result, you may use pandas functions by typing just pd. Then, we are creating the “DataFrame” below this. We assign the “pd.DataFrame” to the “df” variable. We put the name as the column name and place some names for this column.
Here, we add “John”, “Henry”, “Peter”, and “Smith” to this “Name” column. Then comes the “Age” column and we add the age of all those which are “45”, “25”, “60”, and “33”. The third column name is “Pay” here. We add the payment of all of them as “12000”, “35000”, “15000”, and “23500”. After this, we utilize the “print()” statement which prints this data frame in rows and columns. Now, save this code with the name of your choice and the file extension for this will appear automatically.
For obtaining the outcome of this code, we just press “shift+ENTER” or we can also use the run button on this “spyder” tool. When we press the run button from the taskbar, we can get the output on the terminal of the “Spyder” tool. After this, the output will render on the screen. In the given image, you can observe that the data is printed in rows and columns. But we want to print this data in the numpy representation. So, for this we add a few more lines in the above code which are also given below this output screenshot.
For getting the “numpy” representation, we use the “values” attribute with the name of the DataFrame which we have created above. We utilize a variable named “rslt” and assign the “df. values” to this “rslt” variable so it will give the numpy representation of the DataFrame. For printing this “numpy” representation, we utilize the “print ()” statement here.
The screenshot shows that the data is represented as the “numpy”. We get this “numpy” representation with the help of the “values” attribute in our code. There are no labels of the DataFrame in this Numpy representation.
Example # 02:
Now, we are performing another example here for you and we again utilize the “values” attribute in this example. We start our code by importing the “panda” as “pd”. This will help us in utilizing the panda’s function by just putting “pd”. After this, we have a variable named “df1” and we initialize it with a DataFrame by just typing “pd.DataFrame”. We are creating four different columns for this DataFrame as “Name”, “Age”, “Pay”, and “Profession”. We put some names in the “Names” columns and also use the “None” keyword here. This “None” is utilized for defining a null value. We add two names “Alies” and “Peter”, and two “None” keyword in this “Name” column.
Then, we have the “Age”. We add age data as “55”, “64”, and “39”. We also use the “None” for this “Age” column. We add “25000”, “55000”, “28000”, and also one “None” keyword for the “pay” column. Then, comes the “Profession”. We have “IT professional” and “SE engineer” and the remaining two as “None”. First, we print this “DataFrame” which will appear in rows and columns form and then we apply the “values” attribute to the DataFrame “df1” and assigns this to “df2”. We write it as “df1.values” and initialize “df2” with this. Now, we print this “df2” and you will see that it returns the numpy representation of this DataFrame and will remove the labels which we have added in the DataFrame. Save this code and then we can obtain the outcome of this code.
We press “Shift+Enter” and this given output is rendered on the terminal screen. Here, we can easily see the data in the DataFrame which appears in rows and columns. The labels are also mentioned and below the data is rendered in the numpy representation and the labels are removed because we have utilized the “values” attribute with the name of the “DataFrame”. Here, you observe that it renders “NaN” for the “None”.
Example # 03:
Now, we have the third and the last example in which we will utilize the “values” attribute. We again import the “pandas” as “pd”. The reason for importing the “pandas as pd” is already discussed in the above codes. We create a “DataFrame” by putting “pd.DataFrame”. We assign this “pd.DataFrame” to a variable and the name of that variable is “df3”. We add some data in the DataFrame.
As we have discussed, this data will be rendered in the form of rows and columns. We add “David”, “John”, “550”, and “900” in the first row of the Dataframe. We also add “Alies”, “William”, “400”, and “900” in the second row. In the third row, we add “Emma”, “Noah”, “655”, and “900”. Lastly, we add “Alexander”, “Thomas”, “700”, and “900”. Now, we are utilizing the “values” keyword for getting numpy representation. We initialize the “result” variable and initialize it with the “df3. values”. Then, we want to print this numpy representation of DataFrame which we get after applying this “values” attribute. So, we utilize the “print ()” and pass the “result” as the parameter of this function. It will return the “numpy” representation on the output terminal. Now, save this code.
The screenshot which is given below is the numpy representation. We get this output by simply pressing “shift+Enter” which we have also discussed above.
We presented this article to explain the concept of the “pandas values” attribute. We have explained this concept in detail so you will easily understand how to utilize the “values” attribute. We have discussed that the “values” attribute is utilized for getting the numpy representation of the DataFrame. In the numpy representation, the labels are removed. We only obtain the values, not labels. We have explored multiple examples in this article and also explained all lines of codes in detail. We have provided the output of all the codes here as well as the codes.