如何使用Pandas导入excel文件并找到特定的列
在这篇文章中,我们将学习如何将一个excel文件导入到一个数据框架中并找到特定的列。我们假设我们的excel文件是这样的。
Excel表链接:https://drive.google.com/file/d/1x-S0z-gTo-H8byN12_MWLawlCXfMZkm/view?usp=sharing
步骤 :
- 导入Pandas程序
- 创建一个数据框架
- 将Excel数据存储到DataFrame中
- 用head()函数检查特定列并显示
以下是实现情况。
第1步:导入excel文件。
# importing module
import pandas as pd
# creating dataframe
# importing excel file
df = pd.read_excel('Sample_data.xlsx')
df.head()
输出 :
第2步:检查特定的列,用head()显示最上面的5个值。
df[df["Country"] == 'Canada'].head()
输出 :
另一个专栏也有同样的方法。
df[df["Year"] == 2013].head()
输出 :
另一个专栏也有同样的方法。
df[df["Segment"]=='Government'].head()
输出 :