Use DataFrame.loc[] and DataFrame.iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. where Show
Pandas DataFrame is a two-dimensional tabular data structure with labeled axes. i.e. columns and rows. Selecting columns from DataFrame results in a new DataFrame containing only specified selected columns from the original DataFrame. In this article, I will explain how to select single or multiple columns from DataFrame by column labels & index, certain positions of the column, and by range e.t.c with examples. 1. Quick Examples of Select Columns from Pandas DataFrameIf you are in a hurry, below are some quick examples of how to select single or multiple columns from pandas DataFrame by column name and index.
Now, let’s create a DataFrame with a few rows and columns and execute some examples of how to select columns in pandas. Our DataFrame contains column names
Yields below output.
2. Using loc[] to Select Columns by NameBy using pandas.DataFrame.loc[] you can select columns by names or labels. To select the columns by names,
the syntax is
2.1 Select DataFrame Columns by NameTo select single or multiple columns by labels or names, all you need is to provide the names of the columns as a list.
Here we use the
2.2 Select Multiple ColumnsSometimes you may want to select multiple columns from pandas DataFrame, you can do this by passing multiple column names/labels as a list. Note that loc[] also supports multiple conditions when selecting rows based on column values.
2.3 Select DataFrame Columns by RangeWhen you wanted to select columns by the range, provide start and stop column names.
2.4 Select Every Alternate ColumnUsing
3. Pandas iloc[] to Select Column by Index or PositionBy using pandas.DataFrame.iloc[] you can select columns from DataFrame by position/index. ; Remember index starts from 0. You can use 3.1. Select Multiple Columns by Index PositionBelow example retrieves
3.2 Select Columns by Position RangeYou can also slice a DataFrame by a range of positions.
To get the last column use 4. Complete Example of pandas Select ColumnsBelow is a complete example of how to select columns from pandas DataFrame.
ConclusionIn this article, you have learned how to select single or multiple columns from pandas DataFrame using DataFrame.loc[], and DataFrame.iloc[] properties. To understand the similarities and differences of these two refer to pandas loc vs iloc. Happy Learning !! You May Also Like
References
How do I select a column in a list in Python?This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.
How do I extract a column in Python?Extracting Multiple columns from dataframe. Syntax : variable_name = dataframe_name [ row(s) , column(s) ]. Example 1: a=df[ c(1,2) , c(1,2) ]. Explanation : if we want to extract multiple rows and columns we can use c() with row names and column names as parameters. ... . Example 2 : b=df [ c(1,2) , c(“id”,”name”) ]. How do I extract only certain columns in Python?extract one column from dataframe python. import pandas as pd.. input_file = "C:\\....\\consumer_complaints.csv". dataset = pd. read_csv(input_file). df = pd. DataFrame(dataset). cols = [1,2,3,4]. df = df[df. columns[cols]]. How do I select a column in pandas Python?There are three basic methods you can use to select multiple columns of a pandas DataFrame:. Method 1: Select Columns by Index df_new = df. iloc[:, [0,1,3]]. Method 2: Select Columns in Index Range df_new = df. iloc[:, 0:3]. Method 3: Select Columns by Name df_new = df[['col1', 'col2']]. |