Jump to content

Python Pandas Package: Can't understand dataFrame command with 2 arguments


zak100

Recommended Posts

I am trying to understand the pandas package. I understand that it is 2d representation of data.

 

I got the following program from the link: http://www.python-ds.com/python-pandas

import pandas as pd

prices=[

[71,72,73],[77,78,70],[79,72,73],

[74,78,70],[71,88,73],[77,68,70],

[71,72,73],[77,78,70],[79,72,73],

[74,78,70],[71,88,73],[77,68,70]

]

simple_dataframe=pd.DataFrame(prices)

 

I can understand the above code. But I am having problem in understanding the following line:

 

print("Simple Data Frame\n",simple_dataframe)

header_dataframe=pd.DataFrame(prices,columns=['Jan','Feb','Mar'])
Quote

 

Header Data Frame

Jan Feb Mar

0 71 72 73

1 77 78 70

2 79 72 73

3 74 78 70

4 71 88 73

5 77 68 70

6 71 72 73

7 77 78 70

8 79 72 73

9 74 78 70

10 71 88 73

11 77 68 70)

 

I can’t understand the command pd.DataFrame(prices, columns = ['Jan','Feb','Mar']):

 

It is giving the following output:

 

What is the purpose of : prices, columns = [‘Jan’, ‘Feb’, ‘Mar’])

Why are we getting ‘)’ in the output?

 

Somebody please guide me.

 

Zulfi.

Link to comment
Share on other sites

11 hours ago, zak100 said:

I can’t understand the command pd.DataFrame(prices, columns = ['Jan','Feb','Mar']):

It returns a DataFrame with the data from the provided prices variable and column names names 'Jan', 'Feb and ,'Mar'. A god place to get information about Pandas DataFrame parameters is the documentation, for instance https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

 

12 hours ago, zak100 said:

It is giving the following output:

 

What is the purpose of : prices, columns = [‘Jan’, ‘Feb’, ‘Mar’])

 

What output? The empty line? That is expected? The line

header_dataframe=pd.DataFrame(prices,columns=['Jan','Feb','Mar'])

is a declaration. 

Maybe you intended to execute more code? The page you linked to have more code and more outputs than you have provided. Tricky to know what you consider the issue to be here.

Anyway, next issue

12 hours ago, zak100 said:

Why are we getting ‘)’ in the output?

 

Do you mean in the line: "11 77 68 70)" ? A bug in that version of Pandas? A mistake in the web page? I do not know but the page you link to says: Python version 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] Pandas version 0.23.3 Matplotlib version 2.2.2

A test on: Python version 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0] Pandas version 1.0.5 Matplotlib version 3.2.2 results in a last line: 

11 77 68 70

So no parentheses at the end using newer versions.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.