panda读取特定行或列csv数据附代码和数据
一般而言有两种方法,直接读取限定行列,或者读取后再去除。
限制读取法
import pandas as pd
fields = [ "Date", "Start (ET)", "Visitor/Neutral", "PTS", "Home/Neutral", "PTS.1"]
dataset = pd.read_csv('basketball.csv', parse_dates=["Date"], skipinitialspace=True, usecols=fields)
dataset.columns = ["Date", "Start (ET)", "Visitor Team", "VisitorPts", "Home Team", "HomePts"]
dataset.head(6)
或者
import pandas as pd
dataset = pd.read_csv('basketball.csv', parse_dates=["Date"], skipinitialspace=True, usecols=range(6))
dataset.columns = ["Date", "Start (ET)", "Visitor Team", "VisitorPts", "Home Team", "HomePts"]
dataset.head(6)
限定列读取参考
https://stackoverflow.com/questions/40996272/select-2-ranges-of-columns-to-load-read-csv-in-pandas
限定行读取参考
https://stackoverflow.com/questions/52152365/reading-just-range-of-rows-from-one-csv-file-in-python-using-pandas
读取去除法
读取数据
dataset = pd.read_csv(data_filename, parse_dates=["Date"])
dataset.columns = ["Date", "Start (ET)", "Visitor Team", "VisitorPts", "Home Team", "HomePts", "OT?", "Score Type", "Attend", "Notes"]
命名:
dataset.drop(["OT?", "Score Type", "Attend", "Notes"], axis = 1, inplace = True)
dataset.head(6)
去除:
dataset.drop(["OT?", "Score Type", "Attend", "Notes"], axis = 1, inplace = True)
dataset.head(6)
参考资料
Python | Delete rows/columns from DataFrame using Pandas.drop()
数据下载
关注免积分下载
https://download.csdn.net/download/Davidietop/13081366