简体   繁体   English

如何查询数据框中的多个列?

[英]How do I query more than one column in a data frame?

I'm taking a Data Science class that uses Python and this is a questions that stumped me today.我正在上一门使用 Python 的数据科学课程,这是今天困扰我的一个问题。 "How many babies are named “Oliver” in the state of Utah for all years?" “犹他州多年来有多少婴儿被命名为“奥利弗”? To answer this question we were supposed to use data from this set https://raw.githubusercontent.com/byuidatascience/data4names/master/data-raw/names_year/names_year.csv为了回答这个问题,我们应该使用这个集合中的数据https://raw.githubusercontent.com/byuidatascience/data4names/master/data-raw/names_year/names_year.csv

So I started by loading in pandas.所以我从加载熊猫开始。

import pandas as pd

Then I loaded in the data set and created a data frame然后我加载数据集并创建了一个数据框

url='https://raw.githubusercontent.com/byuidatascience/data4names/master/data-raw/names_year/names_year.csv'

names=pd.read_csv(url)

Finally I used the .query() method to single out the data type that I wanted, the name Oliver.最后,我使用 .query() 方法挑选出我想要的数据类型,名称为 Oliver。

oliver=names.query("name == 'Oliver'")

I eventually found the total number of babies that had been named Oliver in Utah using this code我最终使用这段代码找到了犹他州被命名为奥利弗的婴儿总数

total=pd.DataFrame.sum(quiz)

print(total)

but I wasn't sure how to single out the data for both the name and the state, or if that is even possible.但我不确定如何挑选出名称和州的数据,或者是否有可能。 Is there anyone out there that knows of a better way to find this answer?有没有人知道找到这个答案的更好方法?

You have all the code there you just need one more line to Sum accordint to the state:你有所有的代码,你只需要多一行就可以根据状态求和:

print(oliver.UT.sum()) # this will give you the total for the state of UTAH

and forget about the quiz.忘记测验。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算数据框中的多列 - Calculation for more than one column in data frame 如何根据一个以上的列对数据帧进行分组并拆分一列? - how to group a data frame based on more than one column and split one column? 如何根据列名将一个数据框中的列值复制到另一个数据框中? - How do I copy the value of columns in one data frame to another data frame based on column names? 如果我在一列中有多个引用,如何读取 Excel 数据? - How to read the Excel data, if I have more than one reference in one column? 如何满足熊猫数据框中列的特定条件以及检查值是否大于等于 10,000 - How do meet a specific criteria for column in panda data frame as well as checking whether the value is more than equal to 10,000 如何在不止一列上做熊猫样本? - How to do a pandas sample on more than one column? 如何对多个属性(列)进行日志转换 - Python - How to do a log transformation on more than one attribute(column) - Python 当列表类型列在 pandas 数据帧中具有多个值时创建行 - Create rows when list type column has more than one values in a pandas data frame 如何使用 groupby 按多列对数据进行分组 - 如果可能的话 - How to group data by more than one column with groupby - if possible 如何检查熊猫数据框中列中的元素是否在该列中出现两次或两次以上 - How to check if an element in a column in a pandas data frame occurs twice or more than twice in that column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM