一、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()函数:

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行/列差值计算

计算DataFrame行/列的差值使用:

DataFrame.diff(periods=1, axis=0)

其中,

periods:默认值是1,表示两行/列的索引之差,即平移的区间,periods为正整数表示索引大的行/列减去索引小的,反之;axis:运算的轴,默认为0,表示按照行进行计算,axis=1,表示按照列进行计算。

示例:

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

# 默认对行操作,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

# 默认对行操作,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行/列变化率计算

计算DataFrame中行/列的变化率时使用函数:

pd.DataFrame.pct_change(periods, fill_method, limit, freq, **kwargs)

参数含义:

periods : int类型,默认为1,含义同diff函数中的periods;fill_method : str类型, 默认为’pad’,表示在计算变化率之前如何处理空值。'pad’表示使用前一个值进行缺失值填充;limit : int类型, 默认为None,表示填充的最大NA的数量,如果NA的数量大于limit,那么停止填充NA元素;freq : DateOffset, timedelta, 或者 str类型,可选参数,表示时间序列 API 中使用的增量(例如“M”或BDay());**kwargs,其他传递到 DataFrame.shift 或 Series.shift的关键字参数。

函数返回值类型与调用对象一致,为Series或者DataFrame。

Examples:

-----------

**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

# 存在空值时,使用前一个有效值先填充后再计算

>>> 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

# 对列进行变化百分比计算,同时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

【述毕】

笔者独自运营了微信公众号,用于分享个人学习及工作生活趣事,大家可以关注一波。(微信搜索“微思研”)

精彩内容

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。