pandas 学习汇总8 - Series系列,DataFrame数据帧添加删除(行列)( tcy)

添加删除 2018/12/3

1.函数:

s1.append(to_append, ignore_index=False, verify_integrity=False)       #更多序列连接
df.append(other, ignore_index=False, verify_integrity=False, sort=None)#数据帧连接

s1.drop(labels=None)                                                   #删除系列指定的标签
df.drop(labels=None, axis=0, index=None, columns=None, level=None)     #数据帧删除
# 注:操作后原数据不变
2.实例

1.1).Series添加
s1=pd.Series([-1, -2, -3], index=['a', 'b', 'c'])
s2=pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s1['d']=-4                         #系列添加; 原数据改变

# 序列连接
s1.append(s2)                      #s2必须是系列,保留原数据标签
s1.append(s2,ignore_index=True)    #忽略原数据标签,启用新的标签0,1,2,
s1.append(s2,verify_integrity=True)#标签重复报错 

1.2).Series 删除  

del s1['d'] #序列删除;原数据改变
# del s1.d  #错误用法
s2.pop('d') #弹出;参数必须为标签str;原数据改变

s2.drop('d')       #删除数据‘d' ; 原数据不变
s2.drop(['c','d']) #删除数据‘'c',d'
2.1).DataFrame添加
s1=pd.Series([-1, -2, -3], index=['a', 'b', 'c'])
s2=pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
df = pd.DataFrame({'one' : s1,'two' : s2})
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['one','two'])

#添加列
df['three']=pd.Series([10,20,30],index=['a','b','c'])#数据帧添加1列;原数据改变
df['four']=df['one']+df['three']                     #数据帧添加1列;原数据改变

# 添加行
df.append(df2)                                       #添加2行,保留原数据标签 ; 原数据不变
2.2).DataFrame删除
#删除列
del df['four'] #数据帧删除;原数据改变
# del df.four  #错误用法
df.pop('three')#弹出;参数必须为标签str;原数据改变

df.drop(['one'],axis=1)#删除列; 原数据不变
# two
# a 1
# b 2
# c 3
# d 4

#删除行
df.drop(['a','b'])#通过索引删除行; 原数据不变
# one two
# c -3.0 3
# d NaN 4
3.备注-多索引
Drop columns and/or rows of MultiIndex DataFrame
midx = pd.MultiIndex(levels=[['Tom', 'Bob', 'Jam'], ['income', 'weight', 'length']],
labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],[0, 1, 2, 0, 1, 2, 0, 1, 2]])#创建多索引
df = pd.DataFrame(index=midx, columns=['max', 'min'],
data=[[200, 100],[55, 50], [1.5, 1.4], #Tom收入,体重,身高
      [400, 300],[65, 60], [1.6, 1.5], # Bob收入,体重,身高
      [600, 500],[75, 70], [1.8, 1.7]])# Jam收入,体重,身高
df'''
           max min
Tom income 200.0 100.0
    weight 55.0 50.0
    length 1.5 1.4
Bob income 400.0 300.0
    weight 65.0 60.0
    length 1.6 1.5
Jam income 600.0 500.0
    weight 75.0 70.0
    length 1.8 1.7
'''
df.drop(index='Bob', columns='min')
'''''''''''
           max
Tom income 200.0
    weight 55.0
    length 1.5
Jam income 600.0
    weight 75.0
    length 1.8
'''
df.drop(index='length', level=1)
'''''''''
             max min
Tom income 200.0 100.0
    weight 55.0 50.0
Bob income 400.0 300.0
    weight 65.0 60.0
Jam income 600.0 500.0
    weight 75.0 70.0
'''

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84777526