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
Pandas dataframes have a method called filter that will return a new dataframe. Try this
FilteredData = AllData.filter(items=my_dict.keys())
您也可以完全不使用filter方法来执行此操作,如下所示:
FilteredData = AllData[my_dict.keys()]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.