DataFrame
- 一、DataFrame
- 1.创建
- 2.DataFrame常用的方法
- 2.1 df.dtypes:返回每一列的数据类型
- 2.2 df.columns:返回列索引
- 2.3 df.index:返回行索引
- 2.4 df.sort_index(axis=0, ascending=True):对行索引进行排序,默认是升序
- 2.5 df['a']:根据列索引查询,返回一个Seies
- 2.6 df.loc[ ]
- 2.7 df.isnull():判断是否为NaN
- 2.8 df.drop_duplicates():删除重复行
- 2.9 df.dropna(axis='index'):删除值含NaN的行
- 2.10 df.fillna(value='无'):修改值为NaN的值
- 2.11 df.sample(axis='columns', n=2:根据axis随机选择n行或n列
- 2.12 df.pop('d'):根据索引删除列, 返回删除的列
- 2.13 df.shape:返回DataFrame的形状
- 2.14 df.apply(func, axis='columns'):根据func的返回值新增一列
- 2.15 df.set_index(keys='a'):设置某个列为索引
- 2.16 df.query(expr='a==b'):写条件表达式,返回条件成立的DataFrame
- 2.17 df.drop(df.drop(labels=[1, 3], axis=0)):根据标签删除行或列
一、DataFrame
1.创建
- df = pd.DataFrame(data=None, index=None)
例:
data = {
'a': [1, 'c', 'z', 'y', 1],
'b': [1, 'x', 'y', 'm', 1],
# 'c': [np.nan, np.nan, np.nan],
'd': [1, None, 1, None, 1],
# 'e': [1, None, None, None, None],
}
df = pd.DataFrame(data=data,
index=[5, 2, 0, 1, 3])
print(df)
执行结果:
a b d
5 1 1 1.0
2 c x NaN
0 z y 1.0
1 y m NaN
3 1 1 1.0
2.DataFrame常用的方法
2.1 df.dtypes:返回每一列的数据类型
print(df.dtypes)
执行结果:
a object
b object
d float64
dtype: object
2.2 df.columns:返回列索引
print(df.columns)
执行结果:
Index(['a', 'b', 'd'], dtype='object')
2.3 df.index:返回行索引
print(df.index)
执行结果:
Int64Index([5, 2, 0, 1, 3], dtype='int64')
2.4 df.sort_index(axis=0, ascending=True):对行索引进行排序,默认是升序
print(df.sort_index(axis=0, ascending=True))
执行结果:
a b d
0 z y 1.0
1 y m NaN
2 c x NaN
3 1 1 1.0
5 1 1 1.0
2.5 df[‘a’]:根据列索引查询,返回一个Seies
print(df['a'])
执行结果:
5 1
2 c
0 z
1 y
3 1
Name: a, dtype: object
2.6 df.loc[ ]
- df.loc[2:0, ‘a’:‘b’]:根据行列索引查询数据
- df.loc[df[‘a’] != df[‘b’], :]:根据表达式和列索引查询数据
print(df.loc[2:0, 'a':'b'])
print('---------------------------------')
print(df.loc[df['a'] != df['b'], :])
执行结果:
a b
2 c x
0 z y
---------------------------------
a b d
2 c x NaN
0 z y 1.0
1 y m NaN
Process finished with exit code 0
2.7 df.isnull():判断是否为NaN
print(df.isnull())
执行结果:
a b d
5 False False False
2 False False True
0 False False False
1 False False True
3 False False False
2.8 df.drop_duplicates():删除重复行
print(df.drop_duplicates())
执行结果:
a b d
5 1 1 1.0
2 c x NaN
0 z y 1.0
1 y m NaN
2.9 df.dropna(axis=‘index’):删除值含NaN的行
print(df.dropna(axis='index'))
执行结果:
a b d
5 1 1 1.0
0 z y 1.0
3 1 1 1.0
2.10 df.fillna(value=‘无’):修改值为NaN的值
print(df.fillna(value='无'))
执行结果:
a b d
5 1 1 1.0
2 c x 无
0 z y 1.0
1 y m 无
3 1 1 1.0
2.11 df.sample(axis=‘columns’, n=2:根据axis随机选择n行或n列
print(df.sample(axis='columns', n=2))
执行结果:
b a
5 1 1
2 x c
0 y z
1 m y
3 1 1
2.12 df.pop(‘d’):根据索引删除列, 返回删除的列
print(df.pop('d'))
执行结果:
5 1.0
2 NaN
0 1.0
1 NaN
3 1.0
2.13 df.shape:返回DataFrame的形状
print(df.shape)
执行结果:
(5, 3)
2.14 df.apply(func, axis=‘columns’):根据func的返回值新增一列
def get_num(x):
try:
if int(x[0]): # 根据Series的索引,这边的索引是5,2,0,1,3
return '是数字'
except Exception as e:
return '非数字'
df.loc['whether', :] = df.apply(get_num, axis='index')
print(df)
执行结果:
a b d
5 1 1 1.0
2 c x NaN
0 z y 1.0
1 y m NaN
3 1 1 1.0
whether 非数字 非数字 是数字
2.15 df.set_index(keys=‘a’):设置某个列为索引
print(df.set_index(keys='a'))
执行结果:
b d
a
1 1 1.0
c x NaN
z y 1.0
y m NaN
1 1 1.0
2.16 df.query(expr=‘a==b’):写条件表达式,返回条件成立的DataFrame
print(df.query(expr='a==b'))
执行结果:
a b d
5 1 1 1.0
3 1 1 1.0
2.17 df.drop(df.drop(labels=[1, 3], axis=0)):根据标签删除行或列
print(df.drop(labels=[1, 3], axis=0))
执行结果:
a b d
5 1 1 1.0
2 c x NaN
0 z y 1.0