Pandas初级50题练习

文章目录

1.模块导入

import pandas as pd

2.查看pandas版本信息

print(pd.__version__)
1.0.1

注意
Pandas 的数据结构:Pandas 主要有 Series(一维数组),DataFrame(二维数组),Panel(三维数组),Panel4D(四维数组),PanelND(更多维数组)等数据结构。其中 Series 和 DataFrame 应用的最为广泛。

Series 是一维带标签的数组,它可以包含任何数据类型。包括整数,字符串,浮点数,Python 对象等。Series 可以通过标签来定位。
DataFrame 是二维的带标签的数据结构。我们可以通过标签来定位数据。这是 NumPy 所没有的。

3.从列表创建Series序列

ListSeries = [1,2,3,4,5,5.6]
LS3 = pd.Series(ListSeries) # 注意这里的series要大写
LS3
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
dtype: float64

4.从Ndarray创建Series

# 这里会用到NUmpy 先导入
import numpy as np

numpy_4 = np.random.randn(5) # 这里创建一个一维Ndarray随机数组
numpy_4 

index = ['小明','小强','小刚','小王','小q']
LS4 = pd.Series(numpy_4,index = index)
LS4
小明    2.269755
小强   -1.454366
小刚    0.045759
小王   -0.187184
小斯    1.532779
dtype: float64

5.从dict创建Series

dict5 = {'AA':1,'BB':2,'CC':3,'DD':4,'EE':5}
LS5 = pd.Series(dict5)
LS5
AA    1
BB    2
CC    3
DD    4
EE    5
dtype: int64

6.修改Series索引

tip:以下是关于Series的基本操作

print(LS5) # 这里用到前面定义的第5题的数据

LS5.index = ['A','B','C','D','E']
LS5
AA    1
BB    2
CC    3
DD    4
EE    5
dtype: int64





A    1
B    2
C    3
D    4
E    5
dtype: int64

7.Series纵向拼接

print(LS5)
print(LS3)

LS7 = LS5.append(LS3)
LS7

# 顺序前后对比
LS7_1 = LS3.append(LS5)
LS7_1
A    1
B    2
C    3
D    4
E    5
dtype: int64
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
dtype: float64





0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
A    1.0
B    2.0
C    3.0
D    4.0
E    5.0
dtype: float64

8.Series按指定索引删除元素

print(LS7) # 这里用到前面的数据

LS8 = LS7.drop('E')
LS8
A    1.0
B    2.0
C    3.0
D    4.0
E    5.0
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
dtype: float64





A    1.0
B    2.0
C    3.0
D    4.0
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
dtype: float64

9.Series修改指定索引的元素

print(LS7)
LS7['A'] = 100
LS7
A    1.0
B    2.0
C    3.0
D    4.0
E    5.0
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    5.6
dtype: float64





A    100.0
B      2.0
C      3.0
D      4.0
E      5.0
0      1.0
1      2.0
2      3.0
3      4.0
4      5.0
5      5.6
dtype: float64

10.Series按指定索引查找元素

LS7['B']
2.0

11.Series的切片操作

print(LS7)
LS7[:3] # 前3个

A    100.0
B      2.0
C      3.0
D      4.0
E      5.0
0      1.0
1      2.0
2      3.0
3      4.0
4      5.0
5      5.6
dtype: float64





A    100.0
B      2.0
C      3.0
dtype: float64
LS7[:-3] # 后3个
A    100.0
B      2.0
C      3.0
D      4.0
E      5.0
0      1.0
1      2.0
2      3.0
dtype: float64
LS7[1:4] # 中间3个
B    2.0
C    3.0
D    4.0
dtype: float64

12.Series加法运算

tip: 讲运算

这里区别以下NAN,None,Null,
NAN:Not a Number,表示不是一个数,注意一点就是 np.nan == np.nan是不同的,会返回FALSE
None:表示一个空对象,常用来占位,把它看成一个值。
Null:表示为空,不是一个值。

# Series 的加法运算是按照索引计算,如果索引不同则填充为 NaN(空值)
print(LS7)
LS7.add(LS7)
A    100.0
B      2.0
C      3.0
D      4.0
E      5.0
0      1.0
1      2.0
2      3.0
3      4.0
4      5.0
5      5.6
dtype: float64





A    200.0
B      4.0
C      6.0
D      8.0
E     10.0
0      2.0
1      4.0
2      6.0
3      8.0
4     10.0
5     11.2
dtype: float64

13.Series减法运算

LS7.sub(LS7)
A    0.0
B    0.0
C    0.0
D    0.0
E    0.0
0    0.0
1    0.0
2    0.0
3    0.0
4    0.0
5    0.0
dtype: float64

14.Series乘法运算

LS7.mul(LS7)
A    10000.00
B        4.00
C        9.00
D       16.00
E       25.00
0        1.00
1        4.00
2        9.00
3       16.00
4       25.00
5       31.36
dtype: float64

15. Series 除法运算

LS7.div(LS7)
A    1.0
B    1.0
C    1.0
D    1.0
E    1.0
0    1.0
1    1.0
2    1.0
3    1.0
4    1.0
5    1.0
dtype: float64

16. Series 求中位数

LS7.median()
4.0

17. Series 求和

LS7.sum()
134.6

18. Series 求最大值

LS7.max()
100.0

19. Series 求最小值

LS7.min()
1.0

20. 通过 NumPy 数组创建 DataFrame【DataFrame操作部分】

np.random.seed(0) # 设置一个随机种子
date_20 = pd.date_range('today',periods = 6)  # 定义时间序列作为index,行索引
datas = np.random.randn(6,4) # 传入numpy的随机数组  6行4列
columns = ['A','B','C','D'] # 定义列名
df_20 = pd.DataFrame(datas,index = date_20,columns = columns)
df_20
ABCD
2021-01-21 23:41:06.5720781.7640520.4001570.9787382.240893
2021-01-22 23:41:06.5720781.867558-0.9772780.950088-0.151357
2021-01-23 23:41:06.572078-0.1032190.4105990.1440441.454274
2021-01-24 23:41:06.5720780.7610380.1216750.4438630.333674
2021-01-25 23:41:06.5720781.494079-0.2051580.313068-0.854096
2021-01-26 23:41:06.572078-2.5529900.6536190.864436-0.742165

21. 通过字典数组创建 DataFrame

data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
        'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
        'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
# labels = [np.arange(10)+1] # 测试代码
df21 = pd.DataFrame(data, index=labels)
df21
animalagevisitspriority
acat2.51yes
bcat3.03yes
csnake0.52no
ddogNaN3yes
edog5.02no
fcat2.03no
gsnake4.51no
hcatNaN1yes
idog7.02no
jdog3.01no

22. 查看 DataFrame 的数据类型

print(df21.dtypes) # 查看各个变量的数据类型
print(type(df21)) # 查看df21的数据类型
animal       object
age         float64
visits        int64
priority     object
dtype: object
<class 'pandas.core.frame.DataFrame'>

23.预览 DataFrame 的前 5 行数据

df21.head(5) # 5是默认的
animalagevisitspriority
acat2.51yes
bcat3.03yes
csnake0.52no
ddogNaN3yes
edog5.02no

24. 查看 DataFrame 的后 3 行数据

df21.tail(3)
animalagevisitspriority
hcatNaN1yes
idog7.02no
jdog3.01no

25.查看 DataFrame 的索引

df21.index
Index(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], dtype='object')

26. 查看 DataFrame 的列名

df21.columns
Index(['animal', 'age', 'visits', 'priority'], dtype='object')

27. 查看 DataFrame 的数值

df21.values
array([['cat', 2.5, 1, 'yes'],
       ['cat', 3.0, 3, 'yes'],
       ['snake', 0.5, 2, 'no'],
       ['dog', nan, 3, 'yes'],
       ['dog', 5.0, 2, 'no'],
       ['cat', 2.0, 3, 'no'],
       ['snake', 4.5, 1, 'no'],
       ['cat', nan, 1, 'yes'],
       ['dog', 7.0, 2, 'no'],
       ['dog', 3.0, 1, 'no']], dtype=object)

28. 查看 DataFrame 的统计数据

df21.describe()
agevisits
count8.00000010.000000
mean3.4375001.900000
std2.0077970.875595
min0.5000001.000000
25%2.3750001.000000
50%3.0000002.000000
75%4.6250002.750000
max7.0000003.000000

29. DataFrame 转置操作

df21.T # 不会改变原来的df21
abcdefghij
animalcatcatsnakedogdogcatsnakecatdogdog
age2.530.5NaN524.5NaN73
visits1323231121
priorityyesyesnoyesnononoyesnono

30. 对 DataFrame 进行按列排序

df21.sort_values(by = 'visits') # 按visits的值的升序排列
animalagevisitspriority
acat2.51yes
gsnake4.51no
hcatNaN1yes
jdog3.01no
csnake0.52no
edog5.02no
idog7.02no
bcat3.03yes
ddogNaN3yes
fcat2.03no

31. 对 DataFrame 数据切片

print(df21)
df21[1:3] # 取1,2行的所有数据,第三行取不到,默认从0开始。
  animal  age  visits priority
a    cat  2.5       1      yes
b    cat  3.0       3      yes
c  snake  0.5       2       no
d    dog  NaN       3      yes
e    dog  5.0       2       no
f    cat  2.0       3       no
g  snake  4.5       1       no
h    cat  NaN       1      yes
i    dog  7.0       2       no
j    dog  3.0       1       no
animalagevisitspriority
bcat3.03yes
csnake0.52no

32. 对 DataFrame 通过标签查询(单列)

df21['age']
a    2.5
b    3.0
c    0.5
d    NaN
e    5.0
f    2.0
g    4.5
h    NaN
i    7.0
j    3.0
Name: age, dtype: float64
df21.age # 等价的
a    2.5
b    3.0
c    0.5
d    NaN
e    5.0
f    2.0
g    4.5
h    NaN
i    7.0
j    3.0
Name: age, dtype: float64

33. 对 DataFrame 通过标签查询(多列)

df21[['age','visits']]
agevisits
a2.51
b3.03
c0.52
dNaN3
e5.02
f2.03
g4.51
hNaN1
i7.02
j3.01

34. 对 DataFrame 通过位置查询

df21.iloc[1:3] # 
animalagevisitspriority
bcat3.03yes
csnake0.52no

35. DataFrame 副本拷贝

# 生成 DataFrame 副本,方便数据集被多个不同流程使用
df35 = df21.copy()
df35
animalagevisitspriority
acat2.51yes
bcat3.03yes
csnake0.52no
ddogNaN3yes
edog5.02no
fcat2.03no
gsnake4.51no
hcatNaN1yes
idog7.02no
jdog3.01no

36. 判断 DataFrame 元素是否为空

a = df3.isnull()
a
animalagevisitspriorityNo.
1FalseFalseFalseFalseFalse
2FalseFalseFalseFalseFalse
3FalseFalseFalseFalseFalse
4FalseTrueFalseFalseFalse
5FalseFalseFalseFalseFalse
6FalseFalseFalseFalseFalse
7FalseFalseFalseFalseFalse
8FalseTrueFalseFalseFalse
9FalseFalseFalseFalseFalse
10FalseFalseFalseFalseFalse
fTrueFalseTrueTrueTrue
a.describe()
animalagevisitspriorityNo.
count1111111111
unique22222
topFalseFalseFalseFalseFalse
freq109101010

37. 添加列数据

print(df35)
num = pd.Series([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], index=df35.index)

df35['No.'] = num  # 添加以 'No.' 为列名的新数据列
df35
  animal  age  visits priority
a    cat  2.5       1      yes
b    cat  3.0       3      yes
c  snake  0.5       2       no
d    dog  NaN       3      yes
e    dog  5.0       2       no
f    cat  2.0       3       no
g  snake  4.5       1       no
h    cat  NaN       1      yes
i    dog  7.0       2       no
j    dog  3.0       1       no
animalagevisitspriorityNo.
acat2.51yes0
bcat3.03yes1
csnake0.52no2
ddogNaN3yes3
edog5.02no4
fcat2.03no5
gsnake4.51no6
hcatNaN1yes7
idog7.02no8
jdog3.01no9

38. 根据 DataFrame 的下标值进行更改

# 修改第 2 行与第 2 列对应的值 3.0 → 2.0
df3.iat[1, 1] = 2  # 索引序号从 0 开始,这里为 1, 1
df3
animalagevisitspriorityNo.
1cat2.51.0yes0.0
2cat2.03.0yes1.0
3snake0.52.0no2.0
4dogNaN3.0yes3.0
5dog5.02.0no4.0
6cat2.03.0no5.0
7snake4.51.0no6.0
8catNaN1.0yes7.0
9dog7.02.0no8.0
10dog3.01.0no9.0
fNaN1.5NaNNaNNaN

39. 根据 DataFrame 的标签对数据进行修改

df3.loc['f', 'age'] = 1.5
df3
animalagevisitspriorityNo.
1cat2.51.0yes0.0
2cat2.03.0yes1.0
3snake0.52.0no2.0
4dogNaN3.0yes3.0
5dog5.02.0no4.0
6cat2.03.0no5.0
7snake4.51.0no6.0
8catNaN1.0yes7.0
9dog7.02.0no8.0
10dog3.01.0no9.0
fNaN1.5NaNNaNNaN

40. DataFrame 求平均值操作

df3.mean()
age       3.111111
visits    1.900000
No.       4.500000
dtype: float64

41. 对 DataFrame 中任意列做求和操作

df3['visits'].sum()
19.0

42. 将字符串转化为小写字母:

string = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca',
                    np.nan, 'CABA', 'dog', 'cat'])
print(string)
string.str.lower()
0       A
1       B
2       C
3    Aaba
4    Baca
5     NaN
6    CABA
7     dog
8     cat
dtype: object





0       a
1       b
2       c
3    aaba
4    baca
5     NaN
6    caba
7     dog
8     cat
dtype: object

43. 将字符串转化为大写字母

string.str.upper()
0       A
1       B
2       C
3    AABA
4    BACA
5     NaN
6    CABA
7     DOG
8     CAT
dtype: object

44. 对缺失值进行填充

df44 = df35.copy()
print(df44)
df44.fillna(value=3)
  animal  age  visits priority  No.
a    cat  2.5       1      yes    0
b    cat  3.0       3      yes    1
c  snake  0.5       2       no    2
d    dog  NaN       3      yes    3
e    dog  5.0       2       no    4
f    cat  2.0       3       no    5
g  snake  4.5       1       no    6
h    cat  NaN       1      yes    7
i    dog  7.0       2       no    8
j    dog  3.0       1       no    9
animalagevisitspriorityNo.
acat2.51yes0
bcat3.03yes1
csnake0.52no2
ddog3.03yes3
edog5.02no4
fcat2.03no5
gsnake4.51no6
hcat3.01yes7
idog7.02no8
jdog3.01no9

45. 删除存在缺失值的行

df45 = df35.copy()
print(df45)
df45.dropna(how='any')  # 任何存在 NaN 的行都将被删除 还有all模式
  animal  age  visits priority  No.
a    cat  2.5       1      yes    0
b    cat  3.0       3      yes    1
c  snake  0.5       2       no    2
d    dog  NaN       3      yes    3
e    dog  5.0       2       no    4
f    cat  2.0       3       no    5
g  snake  4.5       1       no    6
h    cat  NaN       1      yes    7
i    dog  7.0       2       no    8
j    dog  3.0       1       no    9
animalagevisitspriorityNo.
acat2.51yes0
bcat3.03yes1
csnake0.52no2
edog5.02no4
fcat2.03no5
gsnake4.51no6
idog7.02no8
jdog3.01no9

46. DataFrame 按指定列对齐

left = pd.DataFrame({'key': ['foo1', 'foo2'], 'one': [1, 2]})
right = pd.DataFrame({'key': ['foo2', 'foo3'], 'two': [4, 5]})

print(left)
print(right)

# 按照 key 列对齐连接,只存在 foo2 相同,所以最后变成一行
pd.merge(left, right, on='key')
    key  one
0  foo1    1
1  foo2    2
    key  two
0  foo2    4
1  foo3    5
keyonetwo
0foo224

47.DataFrame 文件操作

df35.to_csv('animal.csv')
print("写入成功.")
写入成功.

48. CSV 文件读取

df_animal = pd.read_csv('animal.csv')
df_animal
Unnamed: 0animalagevisitspriorityNo.
0acat2.51yes0
1bcat3.03yes1
2csnake0.52no2
3ddogNaN3yes3
4edog5.02no4
5fcat2.03no5
6gsnake4.51no6
7hcatNaN1yes7
8idog7.02no8
9jdog3.01no9

49. Excel 写入操作

df35.to_excel('animal.xlsx', sheet_name='Sheet1')
print("写入成功.")
写入成功.

50. Excel 读取操作

pd.read_excel('animal.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
Unnamed: 0animalagevisitspriorityNo.
0acat2.51yes0
1bcat3.03yes1
2csnake0.52no2
3ddogNaN3yes3
4edog5.02no4
5fcat2.03no5
6gsnake4.51no6
7hcatNaN1yes7
8idog7.02no8
9jdog3.01no9

更多推荐