在 Pandas 中打印特定的行和列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50088007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print Specific Rows and Columns in Pandas
提问by royalblue
I am having trouble printing certain values within my CSV.
我在 CSV 中打印某些值时遇到问题。
I have a file that has 9 columns
我有一个有 9 列的文件
Record_id Month Day Year Location_id Animal_id Sex Length Weight
and over 1000 rows.
和超过 1000 行。
I want to print Month , Day , and Year columns when the year is equivalent to 2002.
当年份等于 2002 时,我想打印 Month 、 Day 和 Year 列。
Because I have a lot of data I decided to only work with the first 5 rows where year is equal to 2002.
因为我有很多数据,所以我决定只处理年份等于 2002 的前 5 行。
This is my code:
这是我的代码:
data.df.iloc[0:5, 1:4]
With this I can print the first 5 rows and the 3 columns I desire. However I can't figure out how to filter the year to be 2002
有了这个,我可以打印我想要的前 5 行和 3 列。但是我不知道如何过滤到 2002 年
回答by abdelwaheb moalla
you can start by getting all the rows where year equal to 2002 with
您可以首先获取年份等于 2002 的所有行
filtered_data = df[df["Year"]==2002]
then you can apply your code to get only the first five rows and the three selected columns with
然后你可以应用你的代码来只获取前五行和三个选定的列
filtered_data.iloc[0:5, 1:4]