Selecting multiple columns in a Pandas dataframe
To select multiple columns in a pandas DataFrame, you can pass a list of column names to the indexing operator []
. For example, if you have a DataFrame df
with columns 'a'
, 'b'
, and 'c'
, you can select 'a'
and 'c'
using the following syntax:
df[['a', 'c']]
This will return a new DataFrame with only the columns 'a'
and 'c'
.
You can also use the loc
or iloc
attributes to select multiple columns. For example, to select the same columns using loc
, you would do:
df.loc[:, ['a', 'c']]
This will return a new DataFrame with only the columns 'a'
and 'c'
. The :
in the first index specifies that all rows should be included, and the list of columns ['a', 'c']
specifies which columns to include.
If you want to select multiple columns using their integer indices, you can use the iloc
attribute like this:
df.iloc[:, [0, 2]]
This will return a new DataFrame with the columns at indices 0
and 2
, which are 'a'
and 'c'
in this case.
I hope this helps! Let me know if you have any questions.