Catalog
One 、python What are the various sequences in ?
Two 、 How to access list Elements in list ?
3、 ... and 、 How to access tuple Elements in tuples ?
Four 、 How to access ndarray Elements in an array ?
5、 ... and 、 How to access dict Elements in the dictionary ?
6、 ... and 、 How to access pandas.Serise The elements in ?
7、 ... and 、 How to access pandas.Dataframe The elements in ?
8、 ... and 、 summary
Nine 、 Reference source
list( list )、tuple( Tuples )、ndarray( Array )、dict( Dictionaries )、pandas.Series、pandas.Dataframe, wait .
This article emphasizes python Methods for fetching elements in all containers in , And emphasize multidimensional index , Yes 「 section 」 Less knowledge explanation .
a=[[1,2,3],[4,5,6]]
# Take out the list a An element in
print(a[1][1]) # Index to multidimensional list , You should use multiple side-by-side brackets
print(a[1,1])
# Wrong. ,TypeError: list indices must be integers or slices, not tuple
print(' Sub contents of the list {0[1,1]}'.format([[1,2],[1,2]]))
# Wrong. ,TypeError: list indices must be integers or slices, not str
a = ((1,2,3),(4,5,6))
a[1][1] # Just like the list , All use multiple brackets , A bracket can only put the index value of one dimension
# Output 5
a = np.round(np.random.rand(3,3)*10)
print(a)
# a = [[3. 5. 3.]
# [5. 7. 9.]
# [9. 9. 6.]]
print(a[1,1,1]) # The general usage is to separate the indexes of each dimension with commas
print(a[1:3])
# a[1:3]=[[5. 7. 9.]
# [9. 9. 6.]]
a[1:3][0] # amount to (a[1:3])[0] , That is to say a[1:3] As a whole
# Output is [5. 7. 9.]
a[1:3][1]
# Output is [9. 9. 6.]
dict_2d = {'a': {'a': 1, 'b': 3}, 'b': {'a': 6}}
print(dict_2d['a']['b']) # To the dictionary , You can index them with keys value
# Output is 3
import numpy as np
import pandas as pd
e = pd.Series(data=[1,2,3,4,5],index=['zero','one','two','three','four'])
print(e[1]) # Use the serial number to take out Series The value in
print(e['one']) # Use the index string to fetch Series The value in
print(e[np.array([False,True,False,False,False])]) # Use elements that are Boolean ndarray Fetch target item
# Output results
# 2
# 2
# one 2
# dtype: int64
This part , I mainly refer to it https://blog.csdn.net/wei_lin/article/details/93492252 The content of .
This table is provided by the author of the above link , Quite comprehensive !
Besides , Let me add one more thing .
If you use df It's a pandas.Dataframe object , that df[' Column index '] Yes, you can return this pandas.Dataframe Object , But the object type is not converted to ndarray, still pandas.Dataframe type .
so , except ndarray Is to separate the index data under each dimension with commas , Other similar list、tuple、dict The equal sequence is to use multiple brackets to load the index numbers of each dimension .
Probably because ,ndarray No python Native data type of .
Python tuple Tuple details -C Chinese language network
pandas Detailed explanation (Series piece )_CHenXoo The blog of -CSDN Blog _pandas Medium series
Pandas DataFrame The basic properties of _wei_lin The blog of -CSDN Blog _pd.dataframe