import pandas as pd data = pd.read_csv("person.csv") print(data)输出结果:
ID Name Age City Salary 0 1 Jack 28 Beijing 22000 1 2 Lida 32 Shanghai 19000 2 3 John 43 Shenzhen 12000 3 4 Helen 38 Hengshui 3500通过列索引(标签)读取多列数据。
import pandas as pd #设置"Name"为行索引 data = pd.read_csv("person.csv", index_col ="Name") # 通过列标签选取多列数据 a = data[["City","Salary"]] print(a)输出结果:
City Salary Name Jack Beijing 22000 Lida Shanghai 19000 John Shenzhen 12000 Helen Hengshui 3500再看一组简单的示例:
import pandas as pd info =pd.read_csv("person.csv", index_col ="Name") #获取单列数据,或者以列表的形式传入["Salary"] a =info["Salary"] print(a)输出结果:
Salary Name Jack 22000 Lida 19000 John 12000 Helen 3500
info = pd.DataFrame({'Name': ['Parker', 'Terry', 'Smith', 'William'], 'Year': [2011, 2009, 2014, 2010], 'Leaves': [10, 15, 9, 4]}) #设置Name为行索引 print(info.set_index('Name'))输出结果:
Year Leaves Name Parker 2011 10 Terry 2009 15 Smith 2014 9 William 2010 4
import pandas as pd import numpy as np info = pd.DataFrame([('William', 'C'), ('Smith', 'Java'), ('Parker', 'Python'), ('Phill', np.nan)], index=[1, 2, 3, 4], columns=('name', 'Language')) print(info) print(info.reset_index())输出结果:
重置前: name Language 1 William C 2 Smith Java 3 Parker Python 4 Phill NaN 重置后: index name Language 0 1 William C 1 2 Smith Java 2 3 Parker Python 3 4 Phill NaN
本文链接:http://task.lmcjl.com/news/17343.html