具体属性和方法能够参考官方API。html
pandas.DataFrame(data=None, index=None, columns=None)
参数注释:python
经过等长的字典来建立数据帧,并能够设置数据框的列名和行索引。字典存储的是每列的数据:数据结构
import pandas as pd data = {'user':['小王','小李','小明'],'shcool':['清华','北大','科大'],'class':['数学','历史','计算机']} df = pd.DataFrame(data) df
user | shcool | class | |
---|---|---|---|
0 | 小王 | 清华 | 数学 |
1 | 小李 | 北大 | 历史 |
2 | 小明 | 科大 | 计算机 |
import pandas as pd data = [['小王','小李','小明'],['清华','北大','科大'],['数学','历史','计算机']] columns = ['user','shcool','class'] df = pd.DataFrame(data,columns=columns) df
0 | 1 | 2 | |
---|---|---|---|
0 | 小王 | 小李 | 小明 |
1 | 清华 | 北大 | 科大 |
2 | 数学 | 历史 | 计算机 |
import pandas as pd data = {'user':['小王','小李','小明'],'shcool':['清华','北大','科大'],'class':['数学','历史','计算机']} df = pd.DataFrame.from_dict(data) df
user | shcool | class | |
---|---|---|---|
0 | 小王 | 清华 | 数学 |
1 | 小李 | 北大 | 历史 |
2 | 小明 | 科大 | 计算机 |
import pandas as pd data = {'user':['小王','小李','小明'],'shcool':['清华','北大','科大'],'class':['数学','历史','计算机']} df = pd.DataFrame.from_dict(data)
df.index
RangeIndex(start=0, stop=3, step=1)
df.columns
Index(['user', 'shcool', 'class'], dtype='object')
df.shape
(3, 3)
df.axes
[RangeIndex(start=0, stop=3, step=1), Index(['user', 'shcool', 'class'], dtype='object')]
df.axes[0]
RangeIndex(start=0, stop=3, step=1)
df.axes[1]
Index(['user', 'shcool', 'class'], dtype='object')
df.dtypes
user object shcool object class object dtype: object
df.values
array([['小王', '清华', '数学'], ['小李', '北大', '历史'], ['小明', '科大', '计算机']], dtype=object)
df.values[0]
array(['小王', '清华', '数学'], dtype=object)
import pandas as pd data = {'user':['小王','小李','小明'],'shcool':['清华','北大','科大'],'class':['数学','历史','计算机']} df = pd.DataFrame.from_dict(data)
经过为一个新列赋值来向数据框中追加新列,新列始终处于列名序列的末尾:app
df['createtime']=['1910','1900','1965'] df
user | shcool | class | createtime | |
---|---|---|---|---|
0 | 小王 | 清华 | 数学 | 1910 |
1 | 小李 | 北大 | 历史 | 1900 |
2 | 小明 | 科大 | 计算机 | 1965 |
要制定新列的位置,须要使用insert()函数,该函数向数据框中插入一列,并制定新列的位置:函数
DataFrame.insert(self, loc, column, value)
参数注释:spa
df.insert(1,'score',[98,99,99.5]) df
user | score | shcool | class | createtime | |
---|---|---|---|---|---|
0 | 小王 | 98.0 | 清华 | 数学 | 1910 |
1 | 小李 | 99.0 | 北大 | 历史 | 1900 |
2 | 小明 | 99.5 | 科大 | 计算机 | 1965 |
df.insert(1,'age',22) df
user | age | score | shcool | class | createtime | |
---|---|---|---|---|---|---|
0 | 小王 | 22 | 98.0 | 清华 | 数学 | 1910 |
1 | 小李 | 22 | 99.0 | 北大 | 历史 | 1900 |
2 | 小明 | 22 | 99.5 | 科大 | 计算机 | 1965 |
使用drop()函数来删除行或列:code
DataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
参数注释:orm
df
user | age1 | age | score | shcool | class | createtime | |
---|---|---|---|---|---|---|---|
0 | 小王 | 23 | 22 | 98.0 | 清华 | 数学 | 1910 |
1 | 小李 | 23 | 22 | 99.0 | 北大 | 历史 | 1900 |
2 | 小明 | 23 | 22 | 99.5 | 科大 | 计算机 | 1965 |
df.drop(labels=['age1'],axis=1)
user | age | score | shcool | class | createtime | |
---|---|---|---|---|---|---|
0 | 小王 | 22 | 98.0 | 清华 | 数学 | 1910 |
1 | 小李 | 22 | 99.0 | 北大 | 历史 | 1900 |
2 | 小明 | 22 | 99.5 | 科大 | 计算机 | 1965 |
向数据框的末尾追加数据行:htm
python| DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=None)
功能说明:向dataframe对象中添加新的行,若是添加的列名不在dataframe对象中,将会被看成新的列进行添加对象
import pandas as pd data = {'user':['小王','小李','小明'],'shcool':['清华','北大','科大'],'class':['数学','历史','计算机']} df = pd.DataFrame.from_dict(data) df
user | shcool | class | |
---|---|---|---|
0 | 小王 | 清华 | 数学 |
1 | 小李 | 北大 | 历史 |
2 | 小明 | 科大 | 计算机 |
df.insert(1,'age',22) df
user | age | shcool | class | |
---|---|---|---|---|
0 | 小王 | 22 | 清华 | 数学 |
1 | 小李 | 22 | 北大 | 历史 |
2 | 小明 | 22 | 科大 | 计算机 |
data1 = {'user':['小郑','大王'],'shcool':['清华','医大'],'class':['物理','针灸']} df1=df.append(data1,ignore_index=True) df1
user | age | shcool | class | |
---|---|---|---|---|
0 | 小王 | 22.0 | 清华 | 数学 |
1 | 小李 | 22.0 | 北大 | 历史 |
2 | 小明 | 22.0 | 科大 | 计算机 |
3 | [小郑, 大王] | NaN | [清华, 医大] | [物理, 针灸] |
data2 = {'user':['小郑','大王'],'shcool':['清华','医大'],'class':['物理','针灸']} df2 = pd.DataFrame.from_dict(data2) df3 = df.append(df2) df3
user | age | shcool | class | |
---|---|---|---|---|
0 | 小王 | 22.0 | 清华 | 数学 |
1 | 小李 | 22.0 | 北大 | 历史 |
2 | 小明 | 22.0 | 科大 | 计算机 |
0 | 小郑 | NaN | 清华 | 物理 |
1 | 大王 | NaN | 医大 | 针灸 |
astype(dtype)
函数用于把数据框的列转换为特定的类型,dtype能够是pandas支持的类型,也能够是numpy.dtype,也能够是Python类型:
df.dtypes
user object age int64 shcool object class object dtype: object
df['age']=df['age'].astype('float64')
df.dtypes
user object age float64 shcool object class object dtype: object
更多文章,请关注: