from pandas import Series
s = Series(data=[1,2,3,'four'])
s
复制
0 1
1 2
2 3
3 four
dtype: object
复制
import numpy as np
s = Series(data=np.random.randint(0,100,size=(3,)))
s
复制
0 3
1 43
2 82
dtype: int64
复制
# index用来指定显示索引
# 为什么需要有显示索引
# 显示索引可以增强Series的可读性
s = Series(data=[1,2,3,'four'],index=['a','b','c','d'])
s
复制
a 1
b 2
c 3
d four
dtype: object
复制
dic = {
'语文':100,
'数学':99,
'理综':250
}
s = Series(data=dic)
s
复制
语文 100
数学 99
理综 250
dtype: int64
复制
s[0]
复制
100
复制
s.语文
复制
100
复制
s[0:2]
复制
语文 100
数学 99
dtype: int64
复制
s.shape
复制
(3,)
复制
s.size
复制
3
复制
s.index # 返回索引
复制
Index(['语文','数学','理综'], dtype='object')
复制
s.values # 返回值
复制
array([100, 99, 250])
复制
s.dtype # 元素的类型
复制
dtype('int64')
复制
s = Series(data=[1,2,3,'four'],index=['a','b','c','d'])
s.dtype # 数据类型O表示的是Object(字符串类型)
复制
dtype('O')
复制
s = Series(data=np.random.randint(60,100,size=(10,)))
s
复制
0 79
1 61
2 79
3 60
4 68
5 77
6 76
7 99
8 69
9 89
复制
s.head(3) # 显示前n个数据
复制
0 79
1 61
2 79
dtype: int64
复制
s.tail(3) # 显示后n个元素
复制
7 99
8 69
9 89
dtype: int64
复制
s.unique() # 去重
复制
array([79, 61, 60, 68, 77, 76, 99, 69, 89])
复制
s.isnull() # 用于判断每一个元素是否为空,为空返回True,否则返回False
复制
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
dtype: bool
复制
s.notnull()
复制
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
复制
法则:索引一致的元素进行算数运算否则补空
s1 = Series(data=[1,2,3],index=['a','b','c'])
s2 = Series(data=[1,2,3],index=['a','d','c'])
s = s1 + s2
s
复制
a 2.0
b NaN
c 6.0
d NaN
dtype: float64
复制
s.isnull()
a False
b True
c False
d True
dtype: bool
复制
from pandas import DataFrame
df = DataFrame(data=[[1,2,3],[4,5,6]])
df
复制
df = DataFrame(data=np.random.randint(0,100,size=(6,4)))
df
复制
dic = {
'name':['zhangsan','lisi','wanglaowu'],
'salary':[1000,2000,3000]
}
df = DataFrame(data=dic,index=['a','b','c'])
df
复制
df.values
复制
array([['zhangsan', 1000],
['lisi', 2000],
['wanglaowu', 3000]], dtype=object)
复制
df.columns
复制
Index(['name', 'salary'], dtype='object')
复制
df.index
复制
Index(['a', 'b', 'c'], dtype='object')
复制
df.shape
复制
(3, 2)
复制
============================================
根据以下考试成绩表,创建一个DataFrame,命名为df:
张三 李四
语文 150 0
数学 150 0
英语 150 0
理综 300 0
============================================
复制
dic = {
'张三':[150,150,150,150],
'李四':[0,0,0,0]
}
df = DataFrame(data=dic,index=['语文','数学','英语','理综'])
df
复制
df = DataFrame(data=np.random.randint(60,100,size=(8,4)),columns=['a','b','c','d'])
df
复制
df['a'] # 取单列,如果df有显示的索引,通过索引机制取行或者取列的时候只可以使用显示索引
复制
0 95
1 76
2 69
3 74
4 75
5 67
6 95
7 72
Name: a, dtype: int64
复制
df[['a','c']] #取多列
复制
iloc: 通过隐式索引取行 loc: 通过显示索引取行
复制
# 取单行
df.loc[0]
复制
a 95
b 87
c 83
d 68
Name: 0, dtype: int64
复制
# 取多行
df.iloc[[0,3,5]]
复制
# 取单个元素
df.iloc[0,2]
复制
83
复制
df.loc[0,'a']
复制
95
复制
# 取多个元素
df.iloc[[1,3,5],2]
复制
1 78
3 93
5 66
Name: c, dtype: int64
复制
# 切行
df[0:2]
复制
# 切列
df.iloc[:,0:2]
复制
============================================ 假设ddd是期中考试成绩,ddd2是期末考试成绩,请自由创建ddd2,并将其与ddd相加,求期中期末平均值。 假设张三期中考试数学被发现作弊,要记为0分,如何实现? 李四因为举报张三作弊立功,期中考试所有科目加100分,如何实现? 后来老师发现有一道题出错了,为了安抚学生情绪,给每位学生每个科目都加10分,如何实现? ============================================
复制
dic = {
'张三':[150,150,150,150],
'李四':[0,0,0,0]
}
df = DataFrame(data=dic,index=['语文','数学','英语','理综'])
qizhong = df
qimo = df
复制
(qizhong + qizhong) / 2 # 期中期末的平均值
复制
qizhong.loc['数学','张三'] = 0
qizhong # 将张三的数学成绩修改为0
复制
# 将李四的所有成绩+100
qizhong['李四']+=100
qizhong
复制
qizhong += 10
qizhong # 将所有学生的成绩+10
复制
dic = {
'time':['2010-10-10','2011-11-20','2020-01-10'],
'temp':[33,31,30]
}
df = DataFrame(data=dic)
df
复制
# 查看time列的类型
df['time'].dtype
复制
dtype('O')
复制
import pandas as pd
# 将time列的数据类型转换成时间序列类型
df['time'] = pd.to_datetime(df['time'])
df
复制
df['time']
复制
0 2010-10-10
1 2011-11-20
2 2020-01-10
Name: time, dtype: datetime64[ns]
复制
# 将time列作为源数据的行索引
# 改变原始 df 的数据,inplace=True
df.set_index('time',inplace=True)
df
复制