DataFrame 删除与增减行列

26 篇文章 0 订阅

    1.0 对于行和列都有索引的DataFrame

df=DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])  
df
Out[10]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15

    删除某行

df.drop('a') #默认 axis=0
Out[18]: 
   one  two  three  four
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15
df.drop('a',axis=0)
Out[10]: 
   one  two  three  four
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15

    删除某列

df.drop('one',axis=1) #axis=1 必须有
Out[8]: 
   two  three  four
a    1      2     3
b    5      6     7
c    9     10    11
d   13     14    15

    增加某列

df['new']=list(range(df.shape[0]))#所增加的某列的维度必须与原DataFrame 相一致
df
Out[19]: 
    0   1   2   3  new
0   0   1   2   3    0
1   4   5   6   7    1
2   8   9  10  11    2
3  12  13  14  15    3
df['new']='12'#向 DataFrame 添加一列,该列为同一值

df
Out[25]: 
    0   1   2   3 new  row
0   0   1   2   3  12    1
1   4   5   6   7  12    2
2   8   9  10  11  12    3
3  12  13  14  15  12    4

    计算各列数据总和并作为新列添加到末尾

df=DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])

df['Col_sum'] = df.apply(lambda x: x.sum(), axis=1)

df
Out[36]: 
   one  two  three  four  Col_sum
a    0    1      2     3        6
b    4    5      6     7       22
c    8    9     10    11       38
d   12   13     14    15       54

    计算各行数据总和并作为新行添加到末尾

df.loc['Row_sum'] = df.apply(lambda x: x.sum())

df
Out[39]: 
         one  two  three  four
a          0    1      2     3
b          4    5      6     7
c          8    9     10    11
d         12   13     14    15
Row_sum   24   28     32    36

    对于行和列都没有索引的DataFrame

df=DataFrame(np.arange(16).reshape((4,4)))

    删除某行

df.drop([0])
Out[14]: 
    0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15

df.drop([0],axis=0)
Out[15]: 
    0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15

    删除某列

df.drop([0],axis=1)
Out[16]: 
    1   2   3
0   1   2   3
1   5   6   7
2   9  10  11
3  13  14  15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值