使用 Pandas 读取 CSV 文件的特定列
让我们看看如何使用 Pandas 读取 CSV 文件的特定列。这可以在 pandas.read_csv() 方法。我们将第一个参数作为 CSV 文件传递,第二个参数是关键字usecols中特定列的列表。它将返回特定列的 CSV 文件的数据。
示例1:所用 CSV 文件的链接:link
Python3
# importing the module
import pandas as pd
# read specific columns of csv file using Pandas
df = pd.read_csv("student_scores2.csv", usecols = ['IQ','Scores'])
print(df)
Python3
# importing the module
import pandas as pd
# read specific columns of csv file using Pandas
df = pd.read_csv("student.csv", usecols = ['Hours','Scores','Pass'])
print(df)
Python3
# importing the module
import pandas as pd
# read specific columns of csv file using Pandas
df = pd.read_csv("titanic.csv", usecols = ['Survived','Pclass'])
print(df)
输出
示例 2:所用 CSV 文件的链接:link
蟒蛇3
# importing the module
import pandas as pd
# read specific columns of csv file using Pandas
df = pd.read_csv("student.csv", usecols = ['Hours','Scores','Pass'])
print(df)
输出
示例 3:所用 CSV 文件的链接:link
蟒蛇3
# importing the module
import pandas as pd
# read specific columns of csv file using Pandas
df = pd.read_csv("titanic.csv", usecols = ['Survived','Pclass'])
print(df)
输出: