开发者学堂课程【Python 常用数据科学库:对象操作2】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/546/detail/7483
对象操作2
内容介绍
一、Series 结构的删操作
二、DataFrame 结构的改操作
三、DataFrame 结构的增操作
四、DataFrame 结构的删操作
一、Series 结构的删操作
In [41]:s1
Out[41]: A 101
b 11
d 12
j 500
dtype: int64
//删掉其中的‘A’
In [42]:del s1['A']
s1
Out[42]: b 11
d 12
j 500
dtype: int64
//指定删除(多个一起删)
In [44]: s1. drop(['b','d'], inplace = True)
s1
0ut[44]: j 500
dtype: int64
二、DataFrame 结构的改操作
In [46]:data = [[1, 2,3], [4,5, 6]]
index = ['a','b']
columns = ['A','B','C']
df = pd. DataFrame (data=data, index=index, columns = columns)
df
查操作是类似的
//取一个列
In [47]:df['A']
Out [47]: a 1
b 4
Name: A, dtype: int64
//取两个列
In [49]:df. iloc [0]
Out [49]: A 1
B 2
C 3
Name: a, dtype: int64
In [50]:df.loc['a']
Out[50]: A 1
В 2
C 3
Name: a, dtype: int64
//直接在原数据上改操作
In [52]: df.loc['a']['A']
In [53]: df.loc['a']['A'] = 150
df
//修改索引
In [54]: df.index = ['f','g']
df
三、DataFrame 结构的增操作
In [55]: df.loc['c'] = [1,2,3]
df
In [56]:data = [[1, 2, 3], [4, 5, 6]]
index = ['j','k']
columns = ['A','B','C']
df2 = pd. DataFrame (data=data, index=index,columns = columns)
df2
//将他们连在一起
In [57]: df3 = pd.concat([df,df2])
df3
//或者指定不同的方式
In [60]: df3 = pd.concat ([df, df2],axis = 0)
df3
//增加一列
In [61]: df2['Tang'] = [10,11]
df2
//增加多列
In [62]:df4 = pd. DataFrame ([[10, 11], [12, 13]], index=['j','k'], columns=['D','E'])
df4
//横着进行拼接
In [63]: df5 = pd.concat([df2,df4],axis = 1)
df5
四、DataFrame 结构的删操作
//按行删
In [65]: df5. drop (['j'], axis=0, inplace = True)
df5
//按列删(删一列)
In [66]: del df5['Tang']
df5
//删多列
In [69]: df5.drop (['A','B','C'],axis = 1,inplace = True)
df5