0
点赞
收藏
分享

微信扫一扫

DataFrame 删除与增减行列

何以至千里 2023-01-13 阅读 88


    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


举报

相关推荐

0 条评论