一、查看数据集的特征信息:info( )
#导入所需库
import pandas as pd
import numpy as np
#导入数据
data = pd.read_csv('Salary Data.csv')
print(data.info())
先导入数据,用info()函数查看数据属性的具体信息:数据集行数、属性列编号、属性名、非空列数、数据类型。导入工资预测数据集(https://www.datacastle.cn/dataset_description.html?type=dataset&id=2519),运行结果如下:
RangeIndex: 375 entries, 0 to 374
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Age 373 non-null float64
1 Gender 373 non-null object
2 Education Level 373 non-null object
3 Job Title 373 non-null object
4 Years of Experience 373 non-null float64
5 Salary 373 non-null float64
dtypes: float64(3), object(3)
memory usage: 17.7+ KB
二、查看数据集的所有属性名:columns
# 查看数据的属性列名
print(data.columns)
返回的是数据集中,所有属性列的列名,结果如下:
Index(['Age', 'Gender', 'Education Level', 'Job Title', 'Years of Experience',
'Salary'],
dtype='object')
三、查看各个属性的值内容:value_counts()
(一)统计单个属性的值
一般对object值的属性统计,看的是离散变量的值包括哪些。
# 对object值进行属性值统计查看
# 1、对单个属性进行查看
data['Gender']
print(data['Gender'].value_counts())
结果如下:
Male 194
Female 179
Name: Gender, dtype: int64
(二)统计数据集中所有属性的值
可以利用第二部分的属性名的结果,
Index(['Age', 'Gender', 'Education Level', 'Job Title', 'Years of Experience', 'Salary'],
dtype='object')
代码如下:
# 2、对数据集所有属性进行查看
salary_fea = ['Age', 'Gender', 'Education Level', 'Job Title', 'Years of Experience',
'Salary']
for fea in salary_fea:
print(fea + "的特征分布如下:")
print("{}特征有个{}不同的值".format(fea, data[fea].nunique()))
print(data[fea].value_counts())
最后可以得出数据集所有属性对应的属性值是哪些。结果如下:
Age的特征分布如下:
Age特征有个31不同的值
33.0 24
29.0 23
35.0 22
31.0 21
44.0 21
36.0 20
34.0 17
45.0 17
38.0 15
30.0 15
47.0 15
40.0 13
28.0 13
43.0 12
37.0 12
41.0 12
32.0 12
39.0 12
42.0 11
46.0 10
27.0 9
48.0 9
50.0 8
49.0 8
26.0 7
51.0 5
25.0 4
52.0 3
24.0 1
23.0 1
53.0 1
Name: Age, dtype: int64
Gender的特征分布如下:
Gender特征有个2不同的值
Male 194
Female 179
Name: Gender, dtype: int64
Education Level的特征分布如下:
Education Level特征有个3不同的值
Bachelor's 224
Master's 98
PhD 51
Name: Education Level, dtype: int64
Job Title的特征分布如下:
Job Title特征有个174不同的值
Director of Marketing 12
Director of Operations 11
Senior Business Analyst 10
Senior Marketing Analyst 9
Senior Marketing Manager 9
..
Business Development Manager 1
Customer Service Representative 1
IT Manager 1
Digital Marketing Manager 1
Junior Web Developer 1
Name: Job Title, Length: 174, dtype: int64
Years of Experience的特征分布如下:
Years of Experience特征有个28不同的值
2.0 31
3.0 30
8.0 25
9.0 22
4.0 20
7.0 18
10.0 18
16.0 18
5.0 17
15.0 16
19.0 15
12.0 15
20.0 13
18.0 13
21.0 13
14.0 13
1.5 12
6.0 12
13.0 11
11.0 10
22.0 9
1.0 7
17.0 5
25.0 3
0.0 3
23.0 2
24.0 1
0.5 1
Name: Years of Experience, dtype: int64
Salary的特征分布如下:
Salary特征有个36不同的值
40000.0 31
50000.0 22
95000.0 22
180000.0 20
120000.0 20
90000.0 18
150000.0 18
45000.0 18
160000.0 17
60000.0 17
110000.0 17
170000.0 16
130000.0 14
100000.0 14
140000.0 14
35000.0 13
80000.0 12
55000.0 10
85000.0 10
70000.0 9
65000.0 9
105000.0 6
190000.0 4
75000.0 4
115000.0 3
250000.0 2
200000.0 2
135000.0 2
175000.0 2
125000.0 1
30000.0 1
220000.0 1
185000.0 1
145000.0 1
155000.0 1
350.0 1
Name: Salary, dtype: int64
(三)统计某一列的所有值并输出
column_values = df.iloc[:, 1].values
print(column_values)