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 slice a field and achieve cumulative summation by Python

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces Python how to slice a field and achieve cumulative summation of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this Python how to slice a field and achieve the cumulative sum of the article will have a harvest, let's take a look.

Question: group according to the first column, add the above accumulation to the following grouping, how to achieve?

Train of thought:

1. First pivot perspective, scale is extended to multiple columns

2.cumsum () accumulation

3.melt reverse perspective, restore the original shape

Load the data source:

Import pandas as pddf = pd.read_table (r "D:\ Jupyter\ data\ df_test.txt") df

Data cleaning process:

Years = [before 1980', '1980-1989', '1990-1999', '2000-2009', '2010-2020'] result = (df.pivot (index= "period", columns= "scale", values= "count") .loc [years] .Cumsum () .reset _ index () .melt (id_vars='period') Value_name='count') result.dropna (inplace=True) result ["count"] = result ["count"] .astype ("int16") result.period = result.period.astype ("category") result.period = result.period.cat.reorder_categories (years) result.sort_values (by= ["period", "scale"], inplace=True) result

This is the end of the article on "how to slice a field and achieve a cumulative sum by Python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to slice a field and achieve cumulative summation by Python". If you want to learn more, you are 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.

Share To

Internet Technology

Wechat

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

12
Report