Difference Between [] and [[]] in Python

Difference Between [] and [[]] in Python

I am taking a course on Coursera titled "Data Analysis with Python". I am new to Python. I have some experience with C and MATLAB. That's why I haven't faced that much problem except for one thing.

At first please take a look at the following code.

import pandas as pd

#path of data
path = '
df = pd.read_csv(path)

from sklearn.linear_model import LinearRegression

lm = LinearRegression()

X = df[['highway-mpg']] #'highway-mpg' is a column in the dataframe
Y = df['price'] #'price' is a column in the dataframe

lm.fit(X,Y)

Here while defining X they have used double square brackets [[]] and in the case of Y used []. I know that square brackets are used for listing in Python and nested listing is possible. But it doesn't seem to be a nested list. Can you please differentiate between them?

3

5 Answers

One does slice the dataframe and return a sub-dataframe, the other select exactly one column and gives it as a Series:

  • If you pass a list of string to the brackets [['a', 'c']], you'll obtain DataFrame which contains only the column you asked

  • If you pass a string to the brackets ['a'], you'll obtain only a Series which correponds to the name you give


Example

df = pd.DataFrame([
    {'a': 1, 'b': 2, 'c': 4},
    {'a': 3, 'b': 4, 'c': 6},
])

print(df['a'])
# That is a Series
0    1
1    3
Name: a, dtype: int64
# -------------------------------------------
print(df[['a']])
# That is a DataFrame
   a
0  1
1  3
# -------------------------------------------
print(df[['a', 'c']])
# That is a DataFrame
   a  c
0  1  4
1  3  6

when you want to use or return a single column of the data frame i.e. returns a Series then use df['xyz'] in case you want to return or use multiple columns you can use df[['xyz','abc','ijk']] which returns another data frame with specified columns.

Note

df[['xyz']] this is allowed, but df['xyz','abc','ijk'] this is not allowed.

df1=pd.DataFrame({"Col":[1,2,3],"Row":[2,3,4],"value":[6,5,7],"ree":[0,0,0]})
df1


  Col Row  value ree
0   1   2   6   0
1   2   3   5   0
2   3   4   7   0

df1['Col']
0    1
1    2     # Series object
2    3
Name: Col, dtype: int64

df1[['Col']]
    Col
0   1
1   2      # DataFrame object
2   3

df1[["Col","Row","value"]]
    Col Row value
0   1   2   6
1   2   3   5         # DataFrame object
2   3   4   7

in your case X['h___...'] can also be used as there is only one column

[[]] is generally used to pass multiple columns to retrieve from a dataframe. Another key distinction is it returns a DataFrame

[] is generally used to pass a single column to retrieve from a dataframe. Another key distinction is it returns a Series

This is one of the key differences when indexing a dataframe. Both perform the same intended operation with different return types

3

In Machine Learning and Data Analysis, usually X denotes independent variables and y denotes dependent variable. The goal is to predict single dependent variable y using two or many independent variables X. And, In Pandas [[]] is used to get variable number of columns. That is why [[]] used for X and [] for y

[[colname1,colname2,colnamen]] so you are selecting multiple columns from the dataframe whereas ["target_name"] you only need to specify the name of the column since it's just one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.