数据分析(四)之numpy读取本地数据和索引

数据分析学习线路图

在这里插入图片描述

2、numpy读取本地数据

CSV:Comma-Separated Value,逗号分隔值文件
显示:表格状态
源文件:换行和逗号 分隔行列的格式化文本,每一行的数据表示一条记录

由于csv便于展示,读取和写入,所以很多地方也是用csv的格式存储和传输中小型的数据,为了方便教学,我们会经常操作csv格式的文件,但是操作数据库中的数据也是很容易的实现的

2.1、实现方法

np.loadtxt(fname,dtype=np.float,delimiter=None,skiprows=0,usecols=None,unpack=False)

在这里插入图片描述

import numpy as np

us_file_path = "./youtube_video_data/US_video_data_numbers.csv"
uk_file_path = "./youtube_video_data/GB_video_data_numbers.csv"

t1 = np.loadtxt(us_file_path, delimiter=",", dtype="int", unpack=True)
t2 = np.loadtxt(us_file_path, delimiter=",", dtype="int")

print(t1)
print("*" * 100)
print(t2)
注意观察输出的结果:
[[4394029 7860119 5845909 ...  142463 2162240  515000]
 [ 320053  185853  576597 ...    4231   41032   34727]
 [   5931   26679   39774 ...     148    1384     195]
 [  46245       0  170708 ...     279    4737    4722]]
 ****************************************************************************************************
[[4394029  320053    5931   46245]
 [7860119  185853   26679       0]
 [5845909  576597   39774  170708]
 ...
 [ 142463    4231     148     279]
 [2162240   41032    1384    4737]
 [ 515000   34727     195    4722]]

通过观察可以看到t1、t2虽然读取的同一个数据,但是行列进行了互换
在这里插入图片描述
在这里插入图片描述

3、numpy中的索引和切片

3.1 使用方法

在这里插入图片描述

# coding=utf-8
import numpy as np

us_file_path = "./youtube_video_data/US_video_data_numbers.csv"
uk_file_path = "./youtube_video_data/GB_video_data_numbers.csv"

# t1 = np.loadtxt(us_file_path, delimiter=",", dtype="int", unpack=True)
t2 = np.loadtxt(us_file_path, delimiter=",", dtype="int")

# print(t1)
# print("*" * 100)
print(t2)
#
print("*" * 100)
#
# # 取行
print(t2[2])
#
# # 取连续的多行
# print(t2[2:])
#
# # 取不连续的多行
# print(t2[[2,8,10]])
#
# print(t2[1,:])        # 逗号前取行,逗号后取列
# print(t2[2:,:])     # 取连续的多行
# print(t2[[2,10,3],:])   # 取不连续的多行
#
# # 取列
# print(t2[:,0])
#
# # 取连续的多列
# print(t2[:,2:])
#
# # 取不连续的多列
# print(t2[:,[0,2]])
#
# # 去行和列,取第3行,第四列的值
# a = t2[2,3]
# print(a)
# print(type(a))
#
# # 取多行和多列,取第3行到第五行,第2列到第4列的结果
# # 去的是行和列交叉点的位置
# b = t2[2:5, 1:4]
# print(b)
#
# # 取多个不相邻的点
# # 选出来的结果位置坐标为:(0,0) (2,1) (2,3)
# c = t2[[0, 2, 2], [0, 1, 3]]
# print(c)

csv部分数据如下:这里就不在一 一打印结果,想要测试可以复制一下,自行测试
4394029,320053,5931,46245
7860119,185853,26679,0
5845909,576597,39774,170708
2642103,24975,4542,12829
1168130,96666,568,6666
1311445,34507,544,3040
666169,9985,297,1071
1728614,74062,2180,15297
1338533,69687,678,5643
1056891,29943,878,4046
859289,34485,726,1914
452477,28050,405,2745
258781,8085,303,726
274358,9215,477,838
473691,14740,415,1696
514972,18936,641,3817

3.2 numpy中更多的索引方式

在这里插入图片描述
解答

import numpy as np

t2_temp = np.array(range(24))
print(t2_temp)

t2 = t2_temp.reshape(4, 6)
print(t2)
print("----------------------------------------------------------------------------------- ")
t2[t2 < 10] = 3
print(t2)
结果输出
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
----------------------------------------------------------------------------------- 
[[ 3  3  3  3  3  3]
 [ 3  3  3  3 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

Process finished with exit code 0

在这里插入图片描述
上面的解答见下面三元运算符
在这里插入图片描述
上面的解答见下面clip操作

在这里插入图片描述

请看numpy中的nan和inf

在这里插入图片描述
在这里插入图片描述

那么问题来了,在一组数据中单纯的把nan替换为0,合适么?会带来什么样的影响?

    比如,全部替换为0后,替换之前的平均值如果大于0,替换之后的均值肯定会变小,所以更一般的方式是把缺失的数值替换为均值(中值)或者是直接删除有缺失值的一行

那么问题来了:

   如何计算一组数据的中值或者是均值
   如何删除有缺失数据的那一行(列)[在pandas中介绍]
在这里插入图片描述

例子:将为nan的位置换成该列的均值进行填充

# coding=utf-8
import numpy as np


# print(t1)
def fill_ndarray(t1):
    for i in range(t1.shape[1]):  # 遍历每一列
        temp_col = t1[:, i]  # 当前的一列
        nan_num = np.count_nonzero(temp_col != temp_col)
        if nan_num != 0:  # 不为0,说明当前这一列中有nan
            temp_not_nan_col = temp_col[temp_col == temp_col]  # 当前一列不为nan的array

            # 选中当前为nan的位置,把值赋值为不为nan的均值
            temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
    return t1


if __name__ == '__main__':
    t1 = np.arange(24).reshape((4, 6)).astype("float")
    t1[1, 2:] = np.nan   # 表示将第一行(从0)的第2列和之后的几列都是nan
    print(t1)
    t1 = fill_ndarray(t1)
    print(t1)

输出:
[[ 0.  1.  2.  3.  4.  5.]
 [ 6.  7. nan nan nan nan]
 [12. 13. 14. 15. 16. 17.]
 [18. 19. 20. 21. 22. 23.]]
 
[[ 0.  1.  2.  3.  4.  5.]
 [ 6.  7. 12. 13. 14. 15.]
 [12. 13. 14. 15. 16. 17.]
 [18. 19. 20. 21. 22. 23.]]

Process finished with exit code 0

小结

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40926887/article/details/111143236
今日推荐