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

How to realize pandas alignment Operation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to achieve pandas alignment operation", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to achieve pandas alignment operation"!

1. Arithmetic operation and data alignment

Import numpy as npimport pandas as pd1.1 Series

A1 = pd.Series (np.arange (4), index= ['a2 = pd.Series (np.arange (5), index=]) print (A1) print ("=" * 20) print (a2)

A 0

B 1

C 2

D 3

Dtype: int32

=

A 0

R 1

C 2

U 3

K 4

Dtype: int32

The result becomes a floating-point number when the same index value is added, and the Nan value is returned if the difference is not the same.

A1 + a2

A 0.0

B NaN

C 4.0

D NaN

K NaN

R NaN

U NaN

Dtype: float64

1.2 DataFrame

A3 = pd.DataFrame (np.arange (12) .reshape (3pyr4), index= ['axiaqiaoyuyuzhongyun], columns= [' qlangliao (np.arange (9) .reshape (3d3)) A4 = pd.DataFrame (np.arange (9) .reshape (3pr 3), index= ['aaqu')], columns= ['mordant [']) print (a3) print ("=" * 20) print (A4)

Q w e r

A 0 1 2 3

B 4 5 6 7

C 8 9 10 11

=

M e r

A 0 1 2

U 3 4 5

C 6 7 8

Only those with the same row and column indexes can be operated, otherwise the Nan value is returned.

A3 + A4

E m q r w

A 3.0 NaN NaN 5.0 NaN

B NaN NaN NaN NaN NaN

C 17.0 NaN NaN 19.0 NaN

U NaN NaN NaN NaN NaN

two。 An arithmetic method using padding values

2.1 Series

A1 = pd.Series (np.arange (4), index= ['a2 = pd.Series (np.arange (5), index=]) print (A1) print ("=" * 20) print (a2) print ("=" * 20) print (A1 + a2) # the result becomes a floating point number when the same index values are added, and NAN is returned if different index values are added

A 0

B 1

C 2

D 3

Dtype: int32

=

A 0

R 1

C 2

U 3

K 4

Dtype: int32

=

A 0.0

B NaN

C 4.0

D NaN

K NaN

R NaN

U NaN

Dtype: float64

If you use padding values, you will not return Nan values. If the index values of A1 ~ 2 are the same, then the corresponding values will be arithmetically calculated. If different, a new Series index will be formed as a new row of data.

A1.add (a2 fillworthy value0) # a1+a2 ignores the influence of NAN

A 0.0

B 1.0

C 4.0

D 3.0

K 4.0

R 1.0

U 3.0

Dtype: float64

2.2 DataFrame

A3 = pd.DataFrame (np.arange (12) .reshape (3pr 4), index= ['axiaqiangyuzhongyun'], columns= ['qqyongliao']) a4 = pd.DataFrame (np.arange (9) .reshape (3jin3), index= ['axiong'], columns= ['massively penciled' 'r']) print (A3) print ("=" * 20) print (A4) print ("=" * 20) print (A3 + A4) # can only be calculated if the row index and column index are the same Otherwise, return NAN

Q w e r

A 0 1 2 3

B 4 5 6 7

C 8 9 10 11

=

M e r

A 0 1 2

U 3 4 5

C 6 7 8

=

E m q r w

A 3.0 NaN NaN 5.0 NaN

B NaN NaN NaN NaN NaN

C 17.0 NaN NaN 19.0 NaN

U NaN NaN NaN NaN NaN

NAN is returned only when the values corresponding to the unique rows and columns of two DataFrame indexes are returned. For example, the following b row m column returns the Nan value, whose index value is composed of the unique m of A3 b and A4, and the index value of one of the rows and columns is not unique, then the corresponding DataFrame value is returned. For example, the row Q column in a row is all available in a3 Q column, and the corresponding value in row a Q column in A3 is returned.

# use the padded value, a3.add (a4thecomplete valuevalue 0)

E m q r w

A 3.0 0.0 0.0 5.0 1.0

B 6.0 NaN 4.0 7.0 5.0

C 17.0 6.0 8.0 19.0 9.0

U 4.0 3.0 NaN 5.0 NaN

The letter r indicates that the parameter will be flipped.

1/a3

Q w e r

An inf 1.000000 0.500000 0.333333

B 0.250 0.200000 0.166667 0.142857

C 0.125 0.111111 0.100000 0.090909

A3.rdiv (1) # flip div (division), the result is the same as 1/a3

Q w e r

An inf 1.000000 0.500000 0.333333

B 0.250 0.200000 0.166667 0.142857

C 0.125 0.111111 0.100000 0.090909

Reindex specifies the index and missing values

Replace the column index of A3 with the column index of A4, and return NAN if the index name is different (do not change the original DataFrame index)

A3.reindex (columns=a4.columns))

M e r

A NaN 2 3

B NaN 6 7

C NaN 10 11

# populate NAN with a3.reindex (columns=a4.columns,fill_value=66) # replace all NAN with 66 (any value can be specified)

M e r

A 66 2 3

B 66 6 7

C 66 10 11

Hybrid operation of 3.DataFrame and Series

3.1 broadcast by line

Arr = np.arange (12) .reshape (3pc4) arr

Array ([0, 1, 2, 3]

[4, 5, 6, 7]

[8,9,10,11])

Take out the first line

Arr [0] # take out the first line

Array ([0,1,2,3])

Subtract the first line from each line (broadcast by line)

Arr-arr [0] # subtract the first line from each line (broadcast by line)

Array ([0,0,0,0]

[4, 4, 4, 4]

[8, 8, 8]])

A3 = pd.DataFrame (np.arange (12) .reshape (3pj4), index= ['axiomanagr'], columns= [' qlangliao'])) a3

Q w e r

A 0 1 2 3

B 4 5 6 7

C 8 9 10 11

Location index iloc [m < n] the first parameter m represents the row, and the second parameter n represents the column

S1 = a3.iloc [0] # take out the first line S1

Q 0

W 1

E 2

R 3

Name: a, dtype: int32

Subtract the first line from each line (broadcast by line)

A3-s1 # subtract the first line from each line (broadcast by line)

Q w e r

A 0 0 0

B 4 4 4

C 8 8 8

3.2 broadcast by column

Take out the column Q

S2 = A3 ['q'] # take out Q column S2

A 0

B 4

C 8

Name: q, dtype: int32

The default is row axis=1, specify axis='index' or axis=0, and broadcast by column (A3 all columns minus column Q respectively)

A3.sub (S2 recording axisymmetric index') # defaults to row axis=1, specifies axis='index' (or axis=0), and broadcasts by column (A3 all columns minus column Q respectively)

Q w e r

A 0 1 2 3

B 0 1 2 3

C 0 1 2 3

A3.sub (S2 axis=1) # defaults to row axis=1, which specifies that axis=0 / or axis='index', is broadcast by column (all columns of A3 minus column Q)

Q w e r

A 0 1 2 3

B 0 1 2 3

C 0 1 2 3

A3.sub (S2) # default axis=1

A b c e q r w

A NaN NaN NaN NaN NaN NaN NaN

B NaN NaN NaN NaN NaN NaN NaN

C NaN NaN NaN NaN NaN NaN NaN

Thank you for your reading, the above is the content of "how to achieve pandas alignment operation", after the study of this article, I believe you have a deeper understanding of how to achieve pandas alignment operation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report