一、DataFrame是否為空
判斷整個DataFrame是否為空的方法:
pandas.DataFrame.empty
示例:
df = pd.DataFrame({'fruits':['apple', 'orange', 'watermelon'], 'price':[6, 4, 2]})
df
fruits price
0 apple 6
1 orange 4
2 watermelon 2
if df.empty:
print('=== df為空 ===')
else:
print('=== df非空 ===') # === df非空 ===
df_empty = pd.DataFrame(columns=['fruits', 'price'])
df_empty
Empty DataFrame
Columns: [fruits, price]
Index: []
if df_empty.empty:
print('=== df_empty為空 ===') # === df_empty為空 ===
else:
print('=== df_empty非空 ===')
而判斷具體某個元素是否為NAN,則可以使用isna()
函數(shù):
df = pd.DataFrame({'fruits':['apple', 'orange', 'watermelon'], 'price':[6, 4, 2]})
df
fruits price
0 apple 6
1 orange 4
2 watermelon 2
df.isna()
fruits price
0 False False
1 False False
2 False False
或者使用空值的特征判斷(NAN的一大特征就是不等于本身
):
np.nan != np.nan
True
二、DataFrame行/列差值計(jì)算
計(jì)算DataFrame行/列的差值使用:
DataFrame.diff(periods=1, axis=0)
其中,
-
periods
:默認(rèn)值是1,表示兩行/列的索引之差,即平移的區(qū)間,periods為正整數(shù)表示索引大的行/列減去索引小的,反之; -
axis
:運(yùn)算的軸,默認(rèn)為0,表示按照行進(jìn)行計(jì)算,axis=1,表示按照列進(jìn)行計(jì)算。
示例:
2.1 periods示例
df = pd.DataFrame({'price':[6, 4, 2, 4, 6], 'weight': [12, 43, 23, 3, 5], 'total':[72, 172, 46, 12, 30]})
df
price weight total
0 6 12 72
1 4 43 172
2 2 23 46
3 4 3 12
4 6 5 30
# 默認(rèn)對行操作,periods=1表示后一行減前一行
df.diff(periods=1)
price weight total
0 NaN NaN NaN
1 -2.0 31.0 100.0
2 -2.0 -20.0 -126.0
3 2.0 -20.0 -34.0
4 2.0 2.0 18.0
# periods=2表示做差的兩行間相隔一行
df.diff(periods=2)
price weight total
0 NaN NaN NaN
1 NaN NaN NaN
2 -4.0 11.0 -26.0
3 0.0 -40.0 -160.0
4 4.0 -18.0 -16.0
# 默認(rèn)對行操作,periods=-1表示前一行減后一行
df.diff(periods=-1)
price weight total
0 2.0 -31.0 -100.0
1 2.0 20.0 126.0
2 -2.0 20.0 34.0
3 -2.0 -2.0 -18.0
4 NaN NaN NaN
2.2 axis示例
df
price weight total
0 6 12 72
1 4 43 172
2 2 23 46
3 4 3 12
4 6 5 30
# axis=0表示對行操作
df.diff(periods=1, axis=0)
price weight total
0 NaN NaN NaN
1 -2.0 31.0 100.0
2 -2.0 -20.0 -126.0
3 2.0 -20.0 -34.0
4 2.0 2.0 18.0
# axis=1表示對列操作
df.diff(periods=1, axis=1)
price weight total
0 NaN 6 60
1 NaN 39 129
2 NaN 21 23
3 NaN -1 9
4 NaN -1 25
三、DataFrame行/列變化率計(jì)算
計(jì)算DataFrame中行/列的變化率時使用函數(shù):
pd.DataFrame.pct_change(periods, fill_method, limit, freq, **kwargs)
參數(shù)含義:
-
periods
: int類型,默認(rèn)為1,含義同diff函數(shù)中的periods
; -
fill_method
: str類型, 默認(rèn)為’pad
’,表示在計(jì)算變化率之前如何處理空值。'pad
’表示使用前一個值進(jìn)行缺失值填充; -
limit
: int類型, 默認(rèn)為None,表示填充的最大NA的數(shù)量,如果NA的數(shù)量大于limit,那么停止填充NA元素; -
freq
: DateOffset, timedelta, 或者 str類型,可選參數(shù),表示時間序列 API 中使用的增量(例如“M”或BDay()); -
**kwargs
,其他傳遞到DataFrame.shift
或Series.shift
的關(guān)鍵字參數(shù)。
函數(shù)返回值類型與調(diào)用對象一致,為Series或者DataFrame。
Examples:文章來源:http://www.zghlxwxcb.cn/news/detail-567500.html
-----------
**Series**
-----------
>>> s = pd.Series([90, 91, 85])
>>> s
0 90
1 91
2 85
dtype: int64
>>> s.pct_change()
0 NaN
1 0.011111
2 -0.065934
dtype: float64
>>> s.pct_change(periods=2)
0 NaN
1 NaN
2 -0.055556
dtype: float64
# 存在空值時,使用前一個有效值先填充后再計(jì)算
>>> s = pd.Series([90, 91, None, 85])
>>> s
0 90.0
1 91.0
2 NaN
3 85.0
dtype: float64
>>> s.pct_change(fill_method='ffill')
0 NaN
1 0.011111
2 0.000000
3 -0.065934
dtype: float64
---------------
**DataFrame**
---------------
>>> df = pd.DataFrame({
... 'FR': [4.0405, 4.0963, 4.3149],
... 'GR': [1.7246, 1.7482, 1.8519],
... 'IT': [804.74, 810.01, 860.13]},
... index=['1980-01-01', '1980-02-01', '1980-03-01'])
>>> df
FR GR IT
1980-01-01 4.0405 1.7246 804.74
1980-02-01 4.0963 1.7482 810.01
1980-03-01 4.3149 1.8519 860.13
>>> df.pct_change()
FR GR IT
1980-01-01 NaN NaN NaN
1980-02-01 0.013810 0.013684 0.006549
1980-03-01 0.053365 0.059318 0.061876
# 對列進(jìn)行變化百分比計(jì)算,同時periods=-1表示前一列相對后一列的變化率
>>> df = pd.DataFrame({
... '2016': [1769950, 30586265],
... '2015': [1500923, 40912316],
... '2014': [1371819, 41403351]},
... index=['GOOG', 'APPL'])
>>> df
2016 2015 2014
GOOG 1769950 1500923 1371819
APPL 30586265 40912316 41403351
>>> df.pct_change(axis='columns', periods=-1)
2016 2015 2014
GOOG 0.179241 0.094112 NaN
APPL -0.252395 -0.011860 NaN
筆者獨(dú)自運(yùn)營了微信公眾號,用于分享個人學(xué)習(xí)及工作生活趣事,大家可以關(guān)注一波。(微信搜索“微思研”)文章來源地址http://www.zghlxwxcb.cn/news/detail-567500.html
到了這里,關(guān)于【Python實(shí)用基礎(chǔ)整合(二)】DataFrame是否為空判斷及行/列差值、變化率計(jì)算的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!