In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use the data index in python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use the data index in python" this article.
Preface
Data indexing is the most critical operation in data analysis. In data analysis, it is often necessary to select fixed rows and rows, and you can use the mouse to click in Excel, but when you hit a command, such as python and R, it is not convenient to use the mouse, so you need to flexibly master the index of data. If the data index is not proficient in operation, the code can not be understood, and the subsequent data analysis can not be completed.
Data indexing
Data indexing, also known as data slices and chunks, is a way to split data using different columns or indexes to obtain a specific subset of the data. DataFrame is two-dimensional data, each column is the same type of data, so it is Series, each row can be a different data type, so each row of data is a subset of DataFrame, the data type is still DataFrame.
There are five common methods for pandas indexing, loc iloc ix at iat. Ix is no longer officially recommended. In fact, the author thinks that the index operation of pandas is not as convenient as that of R. But it's enough. At present, loc and iloc are mainly used, and the two are easy to be confused. Here, according to the official method, loc represents location, which is indexed by tags, that is, row or column names, while I in iloc is interpreted as integer, that is, integerlocation is located by numerical index values, that is, line numbers or column numbers.
X.loc [['line name 1', 'line name 2', 'line name 3'.] , ['column name 1', column name 2'.] ] x.iloc [[line number 1, line number 2, line number 3.] , [column number 1, column number 2.] ]
Use numbers to index
Pandas can use row names and column names for indexing, or numbers as indexes. Which method is used depends on the specific usage scenario and flexible use. If there are too many rows and columns, it is more convenient to use row names and column names. If you want to select a range, it is more convenient to use numbers.
If you want to use numbers for indexing, you need to use the iloc tag, with the line before the column, with a comma in the middle, "split, and if all are selected, replace it with a colon": ". The colon can also be used to indicate a certain range.
In particular, note that the index value in python starts at 0. And does not include the right boundary. For example, 0:15 represents the first value to the 15th value.
Use numeric indexing
Mtcars [0:5] mtcars.iloc [0:5,:] mtcars.iloc [1] # negative index mtcars.iloc [:,-1]
Select odd and even rows
# Odd line mtcars.iloc [np.arange (0Magne32)] # even line mtcars.iloc [np.arange (1mem32)]
Index using row name and column name
If you want to select a fixed row, using a period is the most convenient way, similar to the data box index in R using "$", why not? Because data analysis is more likely to process data by column. Note here that because the period in python plays a special role, it is best not to have a period and white space in the column name of the source file. You can use underscore instead.
Mtcars.cylmtcars.cyl.size
Select data directly using row names and column names
Mtcars ['mpg'] mtcars [[' mpg','cyl']] mtcars.loc [:, ['cyl']] mtcars.loc [[' Fiat 128 'mtcars.loc']] mtcars.loc [:, ['disp','hp']]
Delete fixed rows or columns
# Delete the row and column mtcars.drop (columns= ['cyl','mpg']) mtcars.drop (index= [' Valiant']) mtcars [mtcars.indexation] = 5] mtcars [mtcars.indexation needs data Volvo 142E'] above are all the contents of this article entitled "how to use data Index in python". 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.