Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.cumsum()
用於查找任何軸上的累加總和值。每個單元格都填充了到目前為止看到的值的累積和。
用法: DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs)
參數:
axis:{索引(0),列(1)}
skipna:排除NA /空值。如果整個行/列均為NA,則結果為NA
返回:cumsum:係列
範例1:采用cumsum()
函數沿索引軸查找值的累積和。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[5, 3, 6, 4],
"B":[11, 2, 4, 3],
"C":[4, 3, 8, 5],
"D":[5, 4, 2, 8]})
# Print the dataframe
df
輸出:
現在找到索引軸上值的累積和
# To find the cumulative sum
df.cumsum(axis = 0)
輸出:
範例2:采用cumsum()
函數以查找到目前為止沿列軸看到的值的累積和。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = 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 axis
df.cumsum(axis = 1)
輸出:
範例3:采用cumsum()
函數查找迄今在數據幀中沿索引軸看到的值的累積和NaN
DataFrame 中存在的值。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = 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 sum
df.cumsum(axis = 0, skipna = True)
輸出:
輸出是一個數據幀,其中的單元格包含到目前為止沿索引軸看到的值的累積和。任何Nan
DataFrame 中的值將被跳過。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.cumsum()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。