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

Example usage of arithmetic elements between Series and DataFrame in Pandas

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "the example usage of arithmetic elements between Series and DataFrame in Pandas". The explanation in this article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "the example usage of arithmetic elements between Series and DataFrame in Pandas".

As follows:

Import numpy as npimport pandas as pdfrom pandas import Series,DataFrame

1. Series and Series

S1 = Series ([1meme 3d7], index= []) s2 = Series ([2pc4, 6pc8], index= ['axiombinomy, punitive, punitive, fugitive])

Index aligned items are added, and the values of misaligned items take NaN

S1+s21a 3.0b 7.0c 11.0d NaNe NaNdtype: float64

II. DataFrame and DataFrame

Data1 = {'fruit': ['apples', 'pears', 'strawberries'], 'quantity': [3Jing 2jue 5], 'price': [10je 9jue 8]} data2 = {'quantity': [3Jing 2LJ 5J 6], 'price': [10pc9pcje 7]} df1 = DataFrame (data1) df2 = DataFrame (data2)

Calculate after the row and column are aligned at the same time, and take NaN if the corresponding item cannot be found

Print (df1*df2) Price quantity fruit 0 100.0 9.0 NaN1 81.04.0 NaN2 64.025.0 NaN3 NaN NaN NaN

III. Series and DataFrame

1. Using broadcast to realize the operation between DataFrame and a certain line

Print (df2+df2.iloc [0]) # add line 0 to all line price quantities 0 20 61 19 52 18 83 17 9

two。 Use broadcast to realize the operation between DataFrame and a column (specify axis axis=0)

Print (df2.sub (df2.iloc [:, 0], axis=0)) Price quantity 0 0-71 0-72 0-33 0-1

3. If the operation cannot be aligned, NaN is populated.

S = Series, index= ['quantity', 'price', 'weight') print (df2+s) Price quantity weight 0 11 4 NaN1 10 3 NaN2 9 6 NaN3 8 7 NaN Thank you for reading, the above is the content of "the usage of arithmetic elements between Series and DataFrame in Pandas". After the study of this article, I believe you have a deeper understanding of the usage of arithmetic elements between Series and DataFrame in Pandas. The specific use situation still needs to be verified by 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