Python | Панды dataframe.cumsum ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas dataframe.cumsum() is used to find the cumulative sum value over any axis. Each cell is populated with the cumulative sum of the values seen so far.
Syntax: DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs)
Parameters:
axis : {index (0), columns (1)}
skipna : Exclude NA/null values. If an entire row/column is NA, the result will be NAReturns: cumsum : Series
Example #1: Use cumsum() function to find the cumulative sum of the values along the index axis.
# importing pandas as pdimport pandas as pd # Creating the dataframedf = pd.DataFrame({"A":[5, 3, 6, 4], "B":[11, 2, 4, 3], "C":[4, 3, 8, 5], "D":[5, 4, 2, 8]}) # Print the dataframedf |
Выход :
Now find the cumulative sum of the values over the index axis
# To find the cumulative sumdf.cumsum(axis = 0) |
Выход :
Example #2: Use cumsum() function to find the cumulative sum of the values seen so far along the column axis.
# importing pandas as pdimport pandas as pd # Creating the dataframedf = pd.DataFrame({"A":[5, 3, 6, 4], "B":[11, 2, 4, 3], "C":[4, 3, 8, 5], "D":[5, 4, 2, 8]}) # To find the cumulative sum along column axisdf.cumsum(axis = 1) |
Выход :
Example #3: Use cumsum() function to find the cumulative sum of the values seen so far along the index axis in a data frame with NaN value present in dataframe.
# importing pandas as pdimport pandas as pd # Creating the dataframedf = pd.DataFrame({"A":[5, 3, None, 4], "B":[None, 2, 4, 3], "C":[4, 3, 8, 5], "D":[5, 4, 2, None]}) # To find the cumulative sumdf.cumsum(axis = 0, skipna = True) |
Выход :
The output is a dataframe with cells containing the cumulative sum of the values seen so far along the index axis. Any Nan value in the dataframe is skipped.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.