I have dataframe with >100 columns. I am trying to select the columns 0~32 and #83. It seems that 1 slice works fine with the code below.
df_new = df[df.columns[0:32]]
It does not work with 2 slices code below though. How do I fix the problem?
df_new = df[df.columns[0:32, 83]]
To slice the columns, the syntax is df. loc[:,start:stop:step] ; where start is the name of the first column to take, stop is the name of the last column to take, and step as the number of indices to advance after each extraction; for example, you can select alternate columns.
Using df[] & loc[] to Select Multiple Columns by NameBy using df[] & pandas. DataFrame. loc[] you can select multiple columns by names or labels. To select the columns by names, the syntax is df.
You can use the filter function of the pandas dataframe to select columns containing a specified string in column names. The parameter like of the . filter function defines this specific string. If a column name contains the string specified, that column will be selected and dataframe will be returned.
Use the np.r_
indexer in conjunction with iloc
, like this:
df.iloc[:, np.r_[0:32, 83]]
np.r_[0:32, 83]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 83])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With