Just pass a list of column names to index df:
df[['nnn', 'mmm', 'yyy']]
nnn mmm yyy
0 5 5 10
1 3 4 9
2 7 0 8
If you need to handle non-existent column names in your list, try filtering with df.columns.isin -
df.loc[:, df.columns.isin(['nnn', 'mmm', 'yyy', 'zzzzzz'])]
yyy nnn mmm
0 10 5 5
1 9 3 4
2 8 7 0
Answer from coldspeed95 on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.6 documentation
>>> # select columns by name >>> df.filter(items=["one", "three"]) one three mouse 1 3 rabbit 4 6
Top answer 1 of 5
80
Just pass a list of column names to index df:
df[['nnn', 'mmm', 'yyy']]
nnn mmm yyy
0 5 5 10
1 3 4 9
2 7 0 8
If you need to handle non-existent column names in your list, try filtering with df.columns.isin -
df.loc[:, df.columns.isin(['nnn', 'mmm', 'yyy', 'zzzzzz'])]
yyy nnn mmm
0 10 5 5
1 9 3 4
2 8 7 0
2 of 5
6
You can just put mylist inside [] and pandas will select it for you.
mydata_new = mydata[mylist]
Not sure whether your yyy is a typo.
The reason that you are wrong is that you are assigning mydata_new to a new series every time in the loop.
for item in mylist:
mydata_new = mydata[item] # <-
Thus, it will create a series rather than the whole df you want.
If some names in the list is not in your data frame, you can always check it with,
len(set(mylist) - set(mydata.columns)) > 0
and print it out
print(set(mylist) - set(mydata.columns))
Then see if there are typos or other unintended behaviors.
python - Pandas Dataframe Filtering Columns and return column name - Stack Overflow
How can i filter a Pandas DataFrame which looks like this: Head Cat1 Cat2 Cat3 "A" 0 0 1 "B" 1 0 1 So that it returns for every row the Category-Column-Names if ... More on stackoverflow.com
Best way to get names of all numeric columns in Pandas DataFrame?
cols2 is the answer I want (includes boolean as numeric) This has to be a bug in Pandas - the typesystem doesn't consider booleans to be a numeric type, so is_numeric_dtype shouldn't return True for a column of the boolean type. More on reddit.com
Extract pandas dataframe column of tuples into separate columns
Could it be that readydata.index is the wrong size? Here is an example that uses locations.index: >>> locations = pd.DataFrame({'foo' : ['a', 'b', 'c'], 'point': [(1, 2, 3), (2, 3, 4), (3, 4, 5)]}) >>> locations foo point 0 a (1, 2, 3) 1 b (2, 3, 4) 2 c (3, 4, 5) >>> locations[['latitude', 'longitude', 'altitude']] = pd.DataFrame(locations['point'].to_list(), index=locations.index) >>> locations foo point latitude longitude altitude 0 a (1, 2, 3) 1 2 3 1 b (2, 3, 4) 2 3 4 2 c (3, 4, 5) 3 4 5 >>> More on reddit.com
Panda - filter multiple columns by multiple values
You can use .isin(): RawData["Column3"].isin(["AAA", "BBB"]) More on reddit.com
11:11
Filtering of Data in Pandas - Data Science and ML Tutorial by Bikram ...
Filtering Columns and Rows in Pandas | Python Pandas ...
11:54
[Pandas Tutorial] Select, Filter Row or Column from dataframe - ...
17:27
How to filter a pandas DataFrame | 6 HELPFUL METHODS
08:24
Data selection in Pandas with "filter" - YouTube
12:34
How to Filter Pandas Dataframe (subset rows and columns by value) ...
Pandas
pandas.pydata.org › docs › getting_started › intro_tutorials › 03_subset_data.html
How do I select a subset of a DataFrame? — pandas 3.0.6 documentation
To select a single column, use square brackets [] with the column name of the column of interest. For more explanation, see Brackets in Python and pandas. Each column in a DataFrame is a Series. As a single column is selected, the returned object is a pandas Series.
Built In
builtin.com › data-science › pandas-filter
How to Filter Pandas DataFrames | Built In
Filtering in Pandas means to subset (or display) certain rows and columns in a Pandas DataFrame based on specified conditions. The dataframe.filter() function is one method of filtering a DataFrame in Pandas. Lines, or rows, in a Pandas DataFrame can be filtered by using one of the following methods: Filter by logical operators: df.values, df.name, etc.
Easy Tweaks
easytweaks.com › filter-columns-name-in-pandas
How to filter and select columns by name in Pandas?
December 16, 2022 - Verifying that you are not a robot
Towards Data Science
towardsdatascience.com › home › latest › data filtering in pandas
Data filtering in Pandas | Towards Data Science
March 5, 2025 - In addition, Pandas also allows you to obtain a subset of data based on column types and to filter rows with boolean indexing. In this article, we will cover the most common operations for selecting a subset of data from a Pandas data frame: (1) selecting a single column by label, (2) selecting multiple columns by label, (3) selecting columns by data type, (4) selecting a single row by label, (5) selecting multiple rows by label, (6) selecting a single row by position, (7) selecting multiple rows by position, (8) selecting rows and columns simultaneously, (9) selecting a scalar value, and (10) selecting rows using Boolean selection.
Pandas
pandas.pydata.org › pandas-docs › stable › generated › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 2.2.1 documentation
The page has been moved to this page
YoungWonks
youngwonks.com › blog › top-10-ways-to-filter-pandas-dataframe
What is pandas and a pandas dataframe? What are the top 10 ways to filter pandas dataframe? Read our blog to learn more...
June 17, 2022 - It allows us to create subsets from the original dataset by forming smaller dataframes. This makes it easier to study, plot and analyze sections of the data. Therefore, it is important to know these commands in order to use them effectively. In this python tutorial, we are going to learn the top 10 ways to filter pandas dataframe. Making use of specific column names from the dataset, we can choose multiple columns from a pandas dataframe.
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.5 documentation
>>> # select columns by name >>> df.filter(items=["one", "three"]) one three mouse 1 3 rabbit 4 6
Stack Abuse
stackabuse.com › bytes › how-to-select-columns-in-pandas-based-on-a-string-prefix
How to Select Columns in Pandas Based on a String Prefix
August 16, 2023 - The filter() function allows us to select columns based on their labels. We can use the like parameter to specify a string pattern that matches the column names. However, if we want to select columns based on a string prefix, we can use the regex parameter. ... import pandas as pd # Create ...