Take the 2-minute tour ×

I have the following pd.DataFrame:

AllData = 
a@a.6   f@s.2   c@c.2   d@w.4   k@a.3
1       8       3       3       8
4       4       7       4       3
6       8       9       1       6
3       4       5       6       1
7       6       0       8       1

And I would like to create a new pd.DataFrame with only the columns whose names are keys in the following dictionary:

my_dict={a@a.6 : value1, c@c.2 : value2, d@w.4 : value5}

So the new DataFrame would be:

FilteredData = 
a@a.6   c@c.2   d@w.4   
    1       3       3
    4       7       4
    6       9       1
    3       5       6
    7       0       8

What is the most efficient way of doing this?

I have tried to use:

FilteredData = AllData.filter(regex=my_dict.keys)

but unsurprisingly, this didn't work. Any suggestions/advice welcome

Cheers, Alex

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can also do this without the filter method at all like this:

FilteredData = AllData[my_dict.keys()]
share|improve this answer
Is there a difference in the way these two methods are executed? –  user1083734 Jul 3 '13 at 13:59
I'm actually not sure the difference in what happens behind the scene here, but someone here surely must? –  bdiamante Jul 3 '13 at 14:34

Pandas dataframes have a method called filter that will return a new dataframe. Try this

FilteredData = AllData.filter(items=my_dict.keys())
share|improve this answer
No I meant items to itmes, but I understand what you are saying. –  John Jul 3 '13 at 13:57
What is the difference between this method and the one proposed by @bdiamante? –  user1083734 Jul 3 '13 at 14:21
@user1083734 there are differences however I am relatively new to pandas and don't think I can explain them very well. You can look at the DataFrame source on github and see for yourself. The __getitem__ is the "magic" method that bdiamante uses and filter is the method I use. –  John Jul 3 '13 at 14:47

Your Answer


