Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select columns based on columns names containing a specific string in pandas

Tags:

python

pandas

I created a dataframe using the following:

df = pd.DataFrame(np.random.rand(10, 3), columns=['alp1', 'alp2', 'bet1']) 

I'd like to get a dataframe containing every columns from df that have alp in their names. This is only a light version of my problem, so my real dataframe will have more columns.

like image 701
Eric B Avatar asked Apr 26 '17 20:04

Eric B


People also ask

How do I select a column based on column name in Python?

To select a single columns from a dataframe, pass the column name to the [] operator i.e. subscript operator of the dataframe i.e. It will return the column 'Age' of the dataframe (df) as a series object.

How do you select columns based on data type?

Pandas Select columns based on their data typePandas dataframe has the function select_dtypes , which has an include parameter. Specify the datatype of the columns which you want select using this parameter. This can be useful to you if you want to select only specific data type columns from the dataframe.

How do I select only certain columns in pandas?

Selecting columns based on their name 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.


1 Answers

alternative methods:

In [13]: df.loc[:, df.columns.str.startswith('alp')] Out[13]:        alp1      alp2 0  0.357564  0.108907 1  0.341087  0.198098 2  0.416215  0.644166 3  0.814056  0.121044 4  0.382681  0.110829 5  0.130343  0.219829 6  0.110049  0.681618 7  0.949599  0.089632 8  0.047945  0.855116 9  0.561441  0.291182  In [14]: df.loc[:, df.columns.str.contains('alp')] Out[14]:        alp1      alp2 0  0.357564  0.108907 1  0.341087  0.198098 2  0.416215  0.644166 3  0.814056  0.121044 4  0.382681  0.110829 5  0.130343  0.219829 6  0.110049  0.681618 7  0.949599  0.089632 8  0.047945  0.855116 9  0.561441  0.291182 
like image 195
MaxU - stop WAR against UA Avatar answered Sep 22 '22 22:09

MaxU - stop WAR against UA