如何在使用read_csv()或read_excel()后过滤Pandas DataFrame

时间:2016-12-10 20:11:44

标签: python pandas dataframe

刚刚开始学习大熊猫,所以这很可能是一个简单的问题。

有没有办法在读取时通过链接另一个函数或选择器来根据列的值过滤csv或xls文件?例如,我想在一行中做这样的事情。

文件:

var locations: [String]?

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request("http://localhost:3000/locations.json").responseJSON { response in
        guard response.result.error == nil else{
            print("Error: unable to get a list of locations.")
            return
        }
        if let result = response.result.value as? [[String: Any]] {
            self.locations = result.map { $0["name"] as! String }
            self.tableView.reloadData()
        }
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return locations?.count ?? 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    cell.textLabel?.text = locations?[indexPath.row]
    return cell
}

当我在文件中读到我想过滤年龄

Name,Age    
Mike,25   
Joe,19
Mary,30

1 个答案:

答案 0 :(得分:1)

试试这个:

pd.read_csv('file.csv').query('Age >= 21')
相关问题