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.
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.
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.
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.
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
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