In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to read and extract data from csv files by Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Python reads and extracts data from csv files.
The data is saved in the csv file
1. Read data from a csv file
Whether the parameter header=None is available or not
(1) No header=None-- directly treats the first row of the csv table as the header
# read data import pandas as pddata = pd.read_csv ("data1.csv") print (data)
The print result is as follows:
(2) header=None-- automatically adds the first row as the header
# read data import pandas as pddata = pd.read_csv ("data1.csv", header=None) print (data)
The print result is as follows:
two。 Data cutting
(here, according to the format of the csv table, header=None is not written)
(1) get all the columns and store them in an array
# read data import pandas as pddata = pd.read_csv ("data1.csv") # print (data) # ① get all columns And deposit it in an array import numpy as npdata = np.array (data) print (data) # user number sex age (age) annual income (yuan) whether to buy # [15624510 1 19 19000 0] # [15810944 1 35 20000 0] # [15668575 2 26 43 000 0] # [15603246 2 27 57000 0] # [...]]
(2) get the data of the specified column and store it in an array
Method 1: get data,data from csv file []-- need to consider the dimension of the data
# read data import pandas as pddata = pd.read_csv ("data1.csv") print (data) # user number gender age (age) annual income (yuan) whether to buy # (1) get the first column and store it in an array import numpy as npcol_1 = data ["user number"] # get a column Use one-dimensional data data_1 = np.array (col_1) print (data_1) # [15624510 15810944 15603246 15804002 15728773 15598044 15694829 # 15600575 15727311 15570769 15606274 15746139 15704987 15628972 15733883 15617482 15704583 15621083 15649487 15736760 1571458 155990815705113 15631159 2818 15633531 15744529] # (2) get the first and second columns col_12 = data [[user ID, gender]] # To use 2D data data_12 = np.array (col_12) print (data_12) # [[15624510 1] # [15810944 1] # [15668575 2] # [15603246 2] # [..]]
Method 2: usecols= []-- directly write the number of columns obtained
Import pandas as pdimport numpy as npdata_1 = pd.read_csv ("data1.csv", usecols= ["user number"]) data_1 = np.array (data_1) print (data_1) # [[15624510] # [15810944] # [15668575] # [15603246] # [...] # (2) if you get column 1, column 2 data_12 = pd.read_csv ("data1.csv", usecols= ["user number") "gender") data_12 = np.array (data_12) print (data_12) # [[15624510 1] # [15810944 1] # [15668575 2] # [15603246 2] # [..]]
Method 3: iloc []-- essentially a slicing operation
Import pandas as pdimport numpy as npdata = pd.read_csv ("data1.csv") # (1) get the first column data_1 = data.iloc [:, 0] data_1 = np.array (data_1) print (data_1) # [15624510 15810944 15603246 15804002 15728773 15598044 15694829# 15600575 15727311 15570769 15746139 15704987 15628972 15733883 15617482 15704583 15621083 15649487 15714658 155990815705113 1563115792818 1563353529] 2 columns data_12 = data.iloc [:, 0:2] data_12 = np.array (data_12) print (data_12) # [[15624510 1] # [15810944 1] # [15668575 2] # [15603246 2] # [..]] # get the last two columns data_last = data.iloc [: -2:] data_last = np.array (data_last) print (data_last) # [[19000 0] # [20000 0] # [26 43000 0] # [27 57000 0] # [.] At this point, I believe you have a deeper understanding of "how Python reads and extracts data from csv files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.