本文介绍了你怎么过滤多个列的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们考虑男性和女性的数据,我们可能会:

 使用& 运算符,不要忘记用来包装子语句( ):

  males = df [(df [Gender] =='Male')& (df [Year] == 2014)] 

要将数据框存储在 dict 使用for循环:

 来自集合import defaultdict 
dic = {}
for ['男','女']:
dic [g] = defaultdict(dict)
在[2013,2014]中为y:
dic [g ] [y] = df [(df [Gender] == g)& (df [Year] == y)]#将DataFrames存储为字典字典



编辑:


$ b $ < getDF 的演示:


  def getDF(dic,gender,year):
return dic [gender] [year]

print genDF(dic,'male',2014)


To filter a dataframe (df) by a single column, if we consider data with male and females we might:

males = df[df[Gender]=='Male']

Question 1 - But what if the data spanned multiple years and i wanted to only see males for 2014?

In other languages I might do something like:

if A = "Male" and if B = "2014" then 

(except I want to do this and get a subset of the original dataframe in a new dataframe object)

Question 2. How do I do this in a loop, and create a dataframe object for each unique sets of year and gender (i.e. a df for: 2013-Male, 2013-Female, 2014-Male, and 2014-Female

for y in year:

for g in gender:

df = .....
解决方案

Using & operator, don't forget to wrap the sub-statements with ():

males = df[(df[Gender]=='Male') & (df[Year]==2014)]

To store your dataframes in a dict using a for loop:

from collections import defaultdict
dic={}
for g in ['male', 'female']:
  dic[g]=defaultdict(dict)
  for y in [2013, 2014]:
    dic[g][y]=df[(df[Gender]==g) & (df[Year]==y)] #store the DataFrames to a dict of dict

EDIT:

A demo for your getDF:

def getDF(dic, gender, year):
  return dic[gender][year]

print genDF(dic, 'male', 2014)

这篇关于你怎么过滤多个列的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:23