6. 在Series上使用运算符
In[48]: pd.options.display.max_rows = 6
In[49]: 5 + 9 # 加法
Out[49]: 14
In[50]: 4 ** 2 # 幂运算
Out[50]: 16
In[51]: a = 10 # 赋值
In[52]: 5 <= 9 # 小于等于
Out[52]: True
In[53]: 'abcde' + 'fg' # 字符串拼接
Out[53]: 'abcdefg'
In[54]: not (5 <= 9) # 非运算符
Out[54]: False
In[55]: 7 in [1, 2, 6] # in运算符
Out[55]: False
In[56]: set([1,2,3]) & set([2,3,4]) # 求交集
Out[56]: {2, 3}
# 不支持列表和整数间的运算
In[57]: [1, 2, 3] - 3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-57-7ca967348b32> in <module>()
----> 1 [1, 2, 3] - 3
TypeError: unsupported operand type(s) for -: 'list' and 'int'
In[58]: a = set([1,2,3])
a[0] # 集合不支持索引
准备
# 选取imdb_score这列
In[59]: movie = pd.read_csv('data/movie.csv')
imdb_score = movie['imdb_score']
imdb_score
Out[59]: 0 7.9
1 7.1
2 6.8
...
4913 6.3
4914 6.3
4915 6.6
Name: imdb_score, Length: 4916, dtype: float64
# 每列值加1
In[60]: imdb_score + 1
Out[60]: 0 8.9
1 8.1
2 7.8
...
4913 7.3
4914 7.3
4915 7.6
Name: imdb_score, Length: 4916, dtype: float64
# 每列值乘以2.5
In[61]: imdb_score * 2.5
Out[61]: 0 19.75
1 17.75
2 17.00
...
4913 15.75
4914 15.75
4915 16.50
Name: imdb_score, Length: 4916, dtype: float64
# 每列值除以7的余数
In[62]: imdb_score // 7
Out[62]: 0 1.0
1 1.0
2 0.0
...
4913 0.0
4914 0.0
4915 0.0
Name: imdb_score, Length: 4916, dtype: float64
# 判断是否大于7
In[63]: imdb_score > 7
Out[63]: 0 True
1 True
2 False
...
4913 False
4914 False
4915 False
Name: imdb_score, Length: 4916, dtype: bool
# 判断是否等于字符串
In[64]: director = movie['director_name']
In[65]: director == 'James Cameron'
Out[65]: 0 True
1 False
2 False
...
4913 False
4914 False
4915 False
Name: director_name, Length: 4916, dtype: bool
更多
# 利用通用函数实现加法
In[66]: imdb_score.add(1) # imdb_score + 1
Out[66]: 0 8.9
1 8.1
2 7.8
...
4913 7.3
4914 7.3
4915 7.6
Name: imdb_score, Length: 4916, dtype: float64
# 利用通用函数实现乘法
In[67]: imdb_score.mul(2.5) # imdb_score * 2.5
Out[67]: 0 19.75
1 17.75
2 17.00
...
4913 15.75
4914 15.75
4915 16.50
Name: imdb_score, Length: 4916, dtype: float64
# 利用通用函数实现底除
In[68]: imdb_score.floordiv(7) # imdb_score // 7
Out[68]: 0 1.0
1 1.0
2 0.0
...
4913 0.0
4914 0.0
4915 0.0
Name: imdb_score, Length: 4916, dtype: float64
# 利用通用函数实现大于
In[69]: imdb_score.gt(7) # imdb_score > 7
Out[69]: 0 True
1 True
2 False
...
4913 False
4914 False
4915 False
Name: imdb_score, Length: 4916, dtype: bool
# 利用通用函数实现等于
In[70]: director.eq('James Cameron') # director == 'James Cameron'
Out[70]: 0 True
1 False
2 False
...
4913 False
4914 False
4915 False
Name: director_name, Length: 4916, dtype: bool
# 利用通用函数实现取模
In[71]: imdb_score.astype(int).mod(5)
Out[71]: 0 2
1 2
2 1
..
4913 1
4914 1
4915 1
Name: imdb_score, Length: 4916, dtype: int64
# a是int对象
In[72]: a = type(1)
In[73]: type(a)
Out[73]: type
# a是pandas.core.series.Series对象
In[74]: a = type(imdb_score)
In[75]: a([1,2,3])
Out[75]: 0 1
1 2
2 3
dtype: int64