In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use Pandas to read a certain column of a row of data, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
In practice, we often need to find a row or column of data. Here are two methods I use when using Pandas: iloc and loc.
Loc: indexed by row or column name or label
Iloc: find data by the index position of rows and columns
First, let's create a Dataframe and generate data for the following demonstration
Import pandas as pdimport numpy as np # generates DataFramedata = pd.DataFrame (np.arange (30). Reshape (6p5), columns= ['Achilles Pagoda data data']) # writes local data.to_excel ("D:\\ experimental data\\ data.xls", sheet_name= "data") print (data)
1.loc method
The loc method looks for the value we need by the name or label of the row or column.
(1) read the value of the second row
# the value of the second row of the index, with the row label "1" data1 = data.loc [1]
Results:
Note: # the following two grammars have the same effect data.loc [1] = = data.loc [1Magne:]
(2) read the value of the second column
# read all values of the second column data2 = data.loc [:, "B"]
Results:
(3) read a row and a column at the same time
# read row 1, column B corresponding to the value data3 = data.loc [1, "B"]
Results:
(4) read an area of DataFrame
# read values from rows 1 to 3, column B to column D in this area data4 = data.loc [1:3, "B": "D"]
Results:
(5) read according to conditions
# read values greater than 6 in column B data5 = data.loc [data.B > 6] # equivalent to data5 = data [data.B > 6]
Results:
(6) slicing can also be performed.
# for slicing operation, select the value data1 = data.loc of column B greater than 6 in the four-column area of BMageCrector DJE [data.B > 6, ["B", "C", "D", "E"]]
Results:
2.iloc method
The iloc method looks for values by indexing the index position of rows and columns [index, columns].
(1) read the value of the second row
# read the value of the second line, same as the loc method data1 = data.iloc [1] # data1 = data.iloc [1,:], and the effect is the same as above
Results:
(2) read the value of the second column
# read the value of the second column data1 = data.iloc [:, 1]
Results:
(3) read a row and a column at the same time
# read the second row, the value of the second column data1 = data.iloc [1,1]
Results:
(4) slicing operation
# slice operation by index and columns # read line 2 and 3, column 3 and column 4 data1 = data.iloc [1:3, 2:4]
Results:
Note:
The interval here is left closed and right open. You can't get row 4 and column 5 of data.iloc [1:3, 2:4].
Add: use loc and iloc to extract all data In [8]: data.loc [:] # take all the rows in column D of A Magi BMagazine C Out [8]: A B C Da 0 123b 4 567 c 8 9 10 11d 12 13 14 15 In [9]: data.iloc [:,:] # take 0,1 2 Out for all rows of 3 columns: A B C Da 0 12 3b 4 5 6 7c 8 9 10 11d 12 13 14 15
Use the loc function to extract the row of the data according to some data
In [10]: data.loc [data ['A'] = = 0] # extract data data (filter condition: row data where the number is 0 in column A) Out [10]: A B C Da 0123 or above is all the contents of the article "how to use Pandas to read a column and a row of data". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.