程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python | pandas | CSV select the specified column, row and element

編輯:Python

This article summarizes how to use pandas Read csv The specified line of the file 、 Column or element .

Last Modified Date: 2022 / 6 / 17


Python | Pandas | csv Select specified column 、 That's ok 、 Elements

  • Example data
  • Column
    • Specify the label
      • Single column
  • That's ok
  • Reference link


Example data

Reference resources 1, Use pandas Read csv Example data :

data = pd.read_csv(filepath_or_buffer=path, header=None)
print(data.columns)
# Int64Index([0, 1, 2], dtype='int64')
print(data.index.names)
# [None]
print(data)
# 0 1 2
# 0 ABCD NaN All
# 1 EFGH NaN All
# ...
# 1657 OPQR NaN All
# 1658 XYZN NaN All
[1659 rows x 3 columns]

Column

Reference resources 23 Extract the specified column

Specify the label

Single column

  1. loc
col_0 = data.loc[:, 0]
# To extract the first 1 Column content 
# Other columns , And so on 
  1. pd.read_csv(..., usecols=[])
col_0 = pd.read_csv(filepath_or_buffer=path, header=None, usecols=[0])
# To ensure that the data read is correct , Can write excel, Then view the written data 
# writer = pd.Excelwriter('./trial.xlsx')
# data.to_excel(writer, index = False, header = False)
# writer.save()
  1. np.array
col_0 = np.array(data[0])
# The first 1 The column data will be stored as an array 

That's ok

Reference resources 23 Extract the specified column

  1. pd.read_csv(..., nrows=10)
row_0to10 = pd.read_csv(filepath_or_buffer=path, header=None, nrows=10)
# Just before reading 10 That's ok 
  1. pd.read_csv(...,skiprows=9, nrows=5)
row_10to15 = pd.read_csv(..., skiprows=9, nrows=5)
# Ignore before 9 That's ok , Read on 5 That's ok 

Reference link


  1. Python | Pandas | Reading, writing and preliminary processing of various types of files ︎

  2. Python from csv Methods of reading and extracting data in ︎︎

  3. pandas Read the specified line / Several operations of columns ︎︎


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved