Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a double square bracket in Pandas?

Tags:

python

pandas

I am importing these values from a CSV file which I have read in pandas So, my question is that why we are using a double square bracket for X values and a single square bracket for the Y values???

X = df[['sqft_living']]
Y = df['price']
lm = LinearRegression()
lm.fit(X,Y)
lm.score(X,Y)
like image 788
Rohan Avatar asked Jan 14 '20 18:01

Rohan


People also ask

What do 2 brackets mean in Python?

The single and double square brackets are used as indexing operators in R Programming Language. Both of these operators are used for referencing the components of R storage objects either as a subset belonging to the same data type or as an element.

What is the use of shape in pandas?

shape attribute in Pandas enables us to obtain the shape of a DataFrame. For example, if a DataFrame has a shape of (80, 10) , this implies that the DataFrame is made up of 80 rows and 10 columns of data.

What is the difference between a series and a DataFrame?

Series can only contain single list with index, whereas dataframe can be made of more than one series or we can say that a dataframe is a collection of series that can be used to analyse the data.


1 Answers

In python, [] returns a Pandas series object while the [[]] returns a data frame. For the fit() function, it expects the X to be a data frame with 1 or more features. hence, [[]] are used.

like image 189
SaRa Avatar answered Nov 14 '22 23:11

SaRa