Python数据分析(一):Basic、numpy、matplotlib和pandas介绍

本文介绍了Python数据分析的基础知识,包括条件语句、循环、函数,然后详细探讨了numpy库的数组操作,matplotlib库创建各种图表如条形图、直方图、箱线图和地理地图,以及pandas库中Series、DataFrame的使用,包括数据筛选、合并与聚合操作。
摘要由CSDN通过智能技术生成

Basic knowledge

conditionals

  • if
  • elif
  • else
mark= 56
if mark>= 69.5:
    print("distribution")
elif mark>= 59.5:
    print("merit")
elif mark>= 50.0:
    print("pass")
else:
    print("Fail")
pass

Loops

  • for
  • while
  • break
numbers= [1,2,3,4,5,6]
for i in numbers:
    print(i)
1
2
3
4
5
6
number_plus_one= []
number_plus_one= []
for i in numbers:
    number_plus_one.append(i+1)
    print("current number is: "+ str(i))
current number is: 1
current number is: 2
current number is: 3
current number is: 4
current number is: 5
current number is: 6
number_plus_one
[2, 3, 4, 5, 6, 7]
for i in range(1, 8, 2):
    print(i)
1
3
5
7
count= 0
while count< 5:
    print(count)
    count+= 1 
0
1
2
3
4
count= 0
while True:
    print(count)
    count+= 1
    if count>= 5:
        break
0
1
2
3
4

Functions

def my_func():
    print("Hello from my func")
my_func()
Hello from my func
def my_func2(greeting, planet):
    print(greeting+ ' '+ planet+ '!')
my_func2('Hello', 'World')
Hello World!
def add(a, b):
    return a+ b
result= add(1, 2)
print(result)
3
result
3

Python packages, and the numpy package

import numpy as np
np.identity(5)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
from numpy import identity
identity(5)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
from matplotlib import pyplot as plt
plt.plot([1,2,3],[1,4,1],'or')
[<matplotlib.lines.Line2D at 0x208b1c47780>]

在这里插入图片描述

Numpy arrays

import numpy as np
arr= np.array([1.2, 3.14, -6.45])
arr
array([ 1.2 ,  3.14, -6.45])
2* [23,42]
[23, 42, 23, 42]
2* arr
array([  2.4 ,   6.28, -12.9 ])
arr+ 1
array([ 2.2 ,  4.14, -5.45])
arr* -1
array([-1.2 , -3.14,  6.45])
arr** 2
array([ 1.44  ,  9.8596, 41.6025])
arr1= arr
arr2= np.array([1, 2, 3])
print([1, 2 ,3]+ [3, 4, 5]) #list
arr1+ arr2 #numpy array
[1, 2, 3, 3, 4, 5]





array([ 2.2 ,  5.14, -3.45])
arr1* arr2
array([  1.2 ,   6.28, -19.35])
np.sin(1)
0.8414709848078965
np.sin(arr1)
array([ 0.93203909,  0.00159265, -0.16604211])
arr1[0]
1.2
arr[: 2]
array([1.2 , 3.14])

Numpy arrays pt 2

import numpy as np
a= np.array([[1, 2, 3],[4, 5, 6]])
a
array([[1, 2, 3],
       [4, 5, 6]])
a.shape #get the number of rows and cols
(2, 3)
nrows, ncols= a.shape
print(nrows, ncols)
2 3
a.shape= 6
a
array([1, 2, 3, 4, 5, 6])
a.shape= [3, 2]
a
array([[1, 2],
       [3, 4],
       [5, 6]])
a.shape= [2, 3]
a
array([[1, 2, 3],
       [4, 5, 6]])
np.zeros(5)
array([0., 0., 0., 0., 0.])
np.zeros([3, 4])
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
np
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值