Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between df[x], df[[x]], df['x'] , df[['x']] and df.x

Struggling to understand the difference between the 5 examples in the title. Are some use cases for series vs. data frames? When should one be used over the other? Which are equivalent?

like image 793
user8834780 Avatar asked May 12 '18 01:05

user8834780


People also ask

What's the difference between DF and DF [[ ]]?

When you write df["] you are basically accessing a set of number values, but when you use df[["]] you are getting a DataFrame object which is compatible with your code.

What are the different types of data structures in pandas?

Pandas, a data analysis library, supports two data structures: Series: one-dimensional labeled arrays pd. Series(data) DataFrames: two-dimensional data structure with columns, much like a table.

What is DFX in Python?

ezdxf is a Python interface to the DXF (drawing interchange file) format developed by Autodesk, ezdxf allows developers to read and modify existing DXF drawings or create new DXF drawings.

How is series data structure different from a DataFrame data structure?

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

  1. df[x] — index a column using variable x. Returns pd.Series
  2. df[[x]] — index/slice a single-column DataFrame using variable x. Returns pd.DataFrame
  3. df['x'] — index a column named 'x'. Returns pd.Series
  4. df[['x']] — index/slice a single-column DataFrame having only one column named 'x'. Returns pd.DataFrame
  5. df.x — dot accessor notation, equivalent to df['x'] (there are, however, limitations on what x can be named if dot notation is to be successfully used). Returns pd.Series

With single brackets [...] you may only index a single column out as a Series. With double brackets, [[...]], you may select as many columns as you need, and these columns are returned as part of a new DataFrame.


Setup

df
   ID   x
0   0   0
1   1  15
2   2   0
3   3   0
4   4   0
5   5  15

x = 'ID'

Examples

df[x]

0    0
1    1
2    2
3    3
4    4
5    5
Name: ID, dtype: int64

type(df[x])
pandas.core.series.Series

df['x']

0     0
1    15
2     0
3     0
4     0
5    15
Name: x, dtype: int64

type(df['x'])
pandas.core.series.Series

df[[x]]

   ID
0   0
1   1
2   2
3   3
4   4
5   5

type(df[[x]])
pandas.core.frame.DataFrame

df[['x']]

    x
0   0
1  15
2   0
3   0
4   0
5  15

type(df[['x']])
pandas.core.frame.DataFrame

df.x

0     0
1    15
2     0
3     0
4     0
5    15
Name: x, dtype: int64

type(df.x)
pandas.core.series.Series

Further reading
Indexing and Selecting Data

like image 134
cs95 Avatar answered Oct 11 '22 23:10

cs95