Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Index operation of pandas

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The index operation of Pandas is the index object Index1. Indexes in Series and DataFrame are Index objects

Sample code:

Print (type (ser_obj.index)) print (type (df_obj2.index)) print (df_obj2.index)

Running result:

Int64Index ([0,1,2,3], dtype='int64') 2. The index object is immutable, which ensures the security of the data.

Sample code:

# indexed object immutable df_obj2.index [0] = 2

Running result:

-TypeError Traceback (most recent call last) in () 1 # indexed object immutable-> 2 df_obj2.index [0] = 2/Users/Power/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py in _ _ setitem__ (self Key, value) 1402 1403 def _ setitem__ (self, key, value):-> 1404 raise TypeError ("Index does not support mutable operations") 1405 1406 def _ getitem__ (self, key): TypeError: Index does not support mutable operations Common Index species Index, Int64Index, Integer Index MultiIndex, hierarchical Index DatatimeIndex, timestamp Type Series Index 1. Index specifies the row index name

Sample code:

Ser_obj = pd.Series (range (5), index = ['averse,' baked, 'caged,' dumped,'e']) print (ser_obj.head ())

Running result:

A 0b 1c 2d 3e 4dtype: int642. Row index ser_obj ['label'], ser_ OBJ [pos]

Sample code:

Print (ser_obj ['b']) print (ser_obj [2])

Running result:

one hundred and twenty three。 Slice index ser_obj [2:4], ser_obj ['label1':'label3']

Note that when slicing by index name, the

Sample code:

Print (ser_obj [1:3]) print (ser_obj ['bounded Vuitton])

Running result:

B 1c 2dtype: int64b 1c 2d 3dtype: int644. Discontiguous index ser_obj [['label1',' label2', 'label3']]

Sample code:

Print (ser_obj [[0,2,4]]) print (ser_obj [['asides,' e']])

Running result:

A 0c 2e 4dtype: int64a 0e 4dtype: int645. Boolean index

Sample code:

Ser_bool = ser_obj > 2print (ser_bool) print (ser_ OBJ [ser _ bool]) print (ser_ OBJ [ser _ obj > 2])

Running result:

A Falseb Falsec Falsed Truee Truedtype: boold 3e 4dtype: int64d 3e 4dtype: int64DataFrame index 1. Columns specifies the column index name

Sample code:

Import numpy as npdf_obj = pd.DataFrame (np.random.randn (5,4), columns = ['averse,' baked, 'cased,' d']) print (df_obj.head ())

Running result:

A b c d0-0.241678 0.621589 0.843546-0.3831051-0.526918-0.485325 1.124420-0.6531442-1.074163 0.939324-0.309822-0.2091493-0.716816 1.844654-2.123637-1.3234844 0.368212-0.910324 0.064703 0.486016DataFrame Index Colum index (df.columns) Row index (df.index) a b c d 0-0.241678 0.621589 0.843546-0.383105 1-0.526918-0.485325 1.124420-0.653144 2-1.074163 0. 939324-0.309822-0.209149 3-0.716816 1.844654-2.123637-1.323484 4 0.368212-0.910324 0.064703 0.4860162. Column index df_obj [['label']]

Sample code:

Print (df_obj ['a']) # returns Series type print (df_obj [[0]]) # returns DataFrame type, print (type (df_obj [[0]])) # returns DataFrame type is not supported in ipython3, and is not supported in ipython3

Running result:

0-0.2416781-0.5269182-1.0741633-0.7168164 0.368212Name: a, dtype: float643 Discontiguous index df_obj [['label1',' label2']]

Sample code:

Print (df_obj [['averse,' c']]) print (df_obj [1,3]]) # ipython3 is not supported

Running result:

A c0-0.241678 0.8435461-0.526918 1.1244202-1.074163-0.3098223-0.716816-2.1236374 0.368212 0.064703 b d0 0.621589-0.3831051-0.485325-0.6531442 0.939324-0.2091493 1.844654-1.3234844-0.910324 0.486016 Advanced Index: label, location and mixing

There are three advanced indexes of Pandas.

1. Loc tag index DataFrame cannot be sliced directly, but can be sliced through loc

Loc is an index based on the tag signature, which is our custom index name.

Sample code:

# tag index loc# Seriesprint (ser_obj ['bounded VOVIDED']) print (ser_obj.loc ['baked VOBG']) # DataFrameprint (df_obj ['a']) # the first parameter indexes the row, and the second parameter is the column print (df_obj.loc [0:2,'a'])

Running result:

B 1c 2d 3dtype: int64b 1c 2d 3dtype: int640-0.2416781-0.5269182-1.0741633-0.7168164 0.368212Name: a, dtype: float640-0.2416781-0.5269182-1.074163Name: a, dtype: float642. Iloc location indexing works the same as loc, except that it is indexed based on the index number.

Sample code:

# Integer location index iloc# Seriesprint (ser_obj [1:3]) print (ser_obj.iloc [1:3]) # DataFrameprint (df_obj.iloc [0:2, 0]) # Note and df_obj.loc [0:2,'a']

Running result:

B 1c 2dtype: int64b 1c 2dtype: int640-0.2416781-0.526918Name: a, dtype: float643. Ix tag and location hybrid index ix is a combination of the above two. You can use either index numbers or custom indexes, depending on the situation.

If the index has both numbers and English, then this method is not recommended and can easily lead to confusion in positioning.

Sample code:

# mixed index ix# Seriesprint (ser_obj.ix [1:3]) print (ser_obj.ix ['baked mozzarella]) # DataFrameprint (df_obj.loc [0:2,' a']) print (df_obj.ix [0:2, 0])

Running result

B 1c 2dtype: int64b 1c 2dtype: int640-0.2416781-0.5269182-1.074163Name: a, dtype: float64 pay attention to the DataFrame index operation, which can be regarded as the index operation of ndarray

The slice index of the label contains the position at the end

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report