Pandas数据分析笔记(二)

1.DataFrame更改数据

  • 通过loc方法定位到更改的行列,直接写入值覆盖
import pandas as pd
import numpy as np

data = pd.read_csv("D:/DataSet/Demo/iris.csv",encoding="utf-8",names=['Sep_len', 'Sep_wid', 'Pet_len', 'Pet_wid', 'Iris_type'])
print(data.head())

data.loc[0,'Iris_type'] = 'Test_tyep'

print(data.head())
   Sep_len  Sep_wid  Pet_len  Pet_wid    Iris_type
0      5.1      3.5      1.4      0.2  Iris-setosa
1      4.9      3.0      1.4      0.2  Iris-setosa
2      4.7      3.2      1.3      0.2  Iris-setosa
3      4.6      3.1      1.5      0.2  Iris-setosa
4      5.0      3.6      1.4      0.2  Iris-setosa
   Sep_len  Sep_wid  Pet_len  Pet_wid    Iris_type
0      5.1      3.5      1.4      0.2    Test_tyep
1      4.9      3.0      1.4      0.2  Iris-setosa
2      4.7      3.2      1.3      0.2  Iris-setosa
3      4.6      3.1      1.5      0.2  Iris-setosa
4      5.0      3.6      1.4      0.2  Iris-setosa

2.DataFrame删除行列数据

2.1删除列数据

  • drop方法实现删除
  • labels参数接受stirng或array,表示要删除的行和列
  • axis接受0或1,表示轴向,默认为0,0为横向,1为纵向
import pandas as pd
import numpy as np

data = pd.read_csv("D:/DataSet/Demo/iris.csv",encoding="utf-8",names=['Sep_len', 'Sep_wid', 'Pet_len', 'Pet_wid', 'Iris_type'])
print(data.head())
print(data.shape)

data = data.drop(labels='Iris_type',axis=1)

print(data.head())
print(data.shape)
   Sep_len  Sep_wid  Pet_len  Pet_wid    Iris_type
0      5.1      3.5      1.4      0.2  Iris-setosa
1      4.9      3.0      1.4      0.2  Iris-setosa
2      4.7      3.2      1.3      0.2  Iris-setosa
3      4.6      3.1      1.5      0.2  Iris-setosa
4      5.0      3.6      1.4      0.2  Iris-setosa
(150, 5)
   Sep_len  Sep_wid  Pet_len  Pet_wid
0      5.1      3.5      1.4      0.2
1      4.9      3.0      1.4      0.2
2      4.7      3.2      1.3      0.2
3      4.6      3.1      1.5      0.2
4      5.0      3.6      1.4      0.2
(150, 4)

2.2删除行数据

import pandas as pd
import numpy as np

data = pd.read_csv("D:/DataSet/Demo/iris.csv",encoding="utf-8",names=['Sep_len', 'Sep_wid', 'Pet_len', 'Pet_wid', 'Iris_type'])
print(data.head())
print(data.shape)

data = data.drop(labels=0,axis=0)
# data = data.reset_index(drop=True)
print(data.head())
print(data.shape)
   Sep_len  Sep_wid  Pet_len  Pet_wid    Iris_type
0      5.1      3.5      1.4      0.2  Iris-setosa
1      4.9      3.0      1.4      0.2  Iris-setosa
2      4.7      3.2      1.3      0.2  Iris-setosa
3      4.6      3.1      1.5      0.2  Iris-setosa
4      5.0      3.6      1.4      0.2  Iris-setosa
(150, 5)
   Sep_len  Sep_wid  Pet_len  Pet_wid    Iris_type
1      4.9      3.0      1.4      0.2  Iris-setosa
2      4.7      3.2      1.3      0.2  Iris-setosa
3      4.6      3.1      1.5      0.2  Iris-setosa
4      5.0      3.6      1.4      0.2  Iris-setosa
5      5.4      3.9      1.7      0.4  Iris-setosa
(149, 5)

  • 这里为了方便观察没用重置index索引,data = data.reset_index(drop=True)方法可重置索引号
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值