1.DataFrame更改数据
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)方法可重置索引号