热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python刷题系列(9)_Pandas_Series

文章目录PythonSeries1、创建并显示包含数据数组的一维数组类对象2、将pandas系列转换为Python列表类型3、添加、减去、多重和划分两个pandas系列4、比较两个

文章目录

  • Python Series
    • 1、创建并显示包含数据数组的一维数组类对象
    • 2、将pandas系列转换为Python列表类型
    • 3、添加、减去、多重和划分两个pandas系列
    • 4、比较两个series系列的元素
    • 5、将字典转换为series系列
    • 6、将 NumPy 数组转换为series系列
    • 7、更改给定列或系列的数据类型为数值型
      • to_numeric
    • 8、将数据帧的第一列转换为series
    • 9、将给定series转换为数组
    • 10、将series列表转换为一个series
    • 11、对给定series进行排序
    • 12、向现有series添加一些数据
    • 13、根据值和条件创建给定series的子集
    • 14、更改给定series的索引顺序
    • 15、创建给定series的平均值和标准差
    • 16、计算给定series中每个唯一值的频率计数
    • 17、将单词的第一个和最后一个字符转为大写
    • 18、计算给定系列中每个单词的字符数
    • 19、将一系列日期字符串转换为时间序列
    • 20、垂直和水平堆叠两个给定系列


Python Series

1、创建并显示包含数据数组的一维数组类对象

import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(ds)'''
0 2
1 4
2 6
3 8
4 10
dtype: int64
'''


2、将pandas系列转换为Python列表类型

# 创建一个pandas类
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print("Pandas Series and type")
print(ds)
print(type(ds))'''
Pandas Series and type
0 2
1 4
2 6
3 8
4 10
dtype: int64

'''

将series变成list类型,下面两种得到的结果都相同:
方法1:X.tolist()
方法2:list(X)

#方法一
print(ds.tolist())
print(type(ds.tolist()))
#方法二
print(list(ds))
print(type(list(ds)))'''
[2, 4, 6, 8, 10]

'''


3、添加、减去、多重和划分两个pandas系列

编写一个熊猫程序来添加,减去,多重和除以两个熊猫系列。

import pandas as pd
ds1 = pd.Series([2, 4, 6])
ds2 = pd.Series([1, 3, 5])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)'''
Add two Series:
0 3
1 7
2 11
dtype: int64
Subtract two Series:
0 1
1 1
2 1
dtype: int64
Multiply two Series:
0 2
1 12
2 30
dtype: int64
Divide Series1 by Series2:
0 2.000000
1 1.333333
2 1.200000
dtype: float64
'''

这里如果两个series的元素个数不一致的话,仍然可以进行加减乘除,不会报错,并且缺少的部分会使用NaN进行填充:

import pandas as pd
ds1 = pd.Series([2, 4, 6])
ds2 = pd.Series([1, 3])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)'''
Add two Series:
0 3.0
1 7.0
2 NaN
dtype: float64
Subtract two Series:
0 1.0
1 1.0
2 NaN
dtype: float64
Multiply two Series:
0 2.0
1 12.0
2 NaN
dtype: float64
Divide Series1 by Series2:
0 2.000000
1 1.333333
2 NaN
dtype: float64
'''


4、比较两个series系列的元素

编写一个熊猫程序来比较两个熊猫系列的元素。

import pandas as pd
ds1 = pd.Series([2, 4, 6])
ds2 = pd.Series([1, 7, 6])
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Compare the elements of the said Series:")
print("Equals:")
print(ds1 == ds2)
print("Greater than:")
print(ds1 > ds2)
print("Less than:")
print(ds1 < ds2)&#39;&#39;&#39;
Series1:
0 2
1 4
2 6
dtype: int64
Series2:
0 1
1 7
2 6
dtype: int64
Compare the elements of the said Series:
Equals:
0 False
1 False
2 True
dtype: bool
Greater than:
0 True
1 False
2 False
dtype: bool
Less than:
0 False
1 True
2 False
dtype: bool
&#39;&#39;&#39;


5、将字典转换为series系列

编写一个程序&#xff0c;将字典转换为熊猫系列。
示例字典&#xff1a; d1 &#61; {‘a’&#xff1a; 100&#xff0c; ‘b’&#xff1a; 200&#xff0c; ‘c’&#xff1a;300&#xff0c; ‘d’&#xff1a;400&#xff0c; ‘e’&#xff1a;800}

使用pd.Series(d1)&#xff1a;会将字典的键变成index&#xff0c;值对应值

import pandas as pd
d1 &#61; {&#39;a&#39;: 100, &#39;b&#39;: 200, &#39;c&#39;:300, &#39;d&#39;:400, &#39;e&#39;:800}
print("Original dictionary:")
print(d1)
new_series &#61; pd.Series(d1)
print("Converted series:")
print(new_series)&#39;&#39;&#39;
Original dictionary:
{&#39;a&#39;: 100, &#39;b&#39;: 200, &#39;c&#39;: 300, &#39;d&#39;: 400, &#39;e&#39;: 800}
Converted series:
a 100
b 200
c 300
d 400
e 800
dtype: int64
&#39;&#39;&#39;


6、将 NumPy 数组转换为series系列

编写一个熊猫程序&#xff0c;将NumPy数组转换为熊猫系列。

示例 NumPy 数组&#xff1a;d1 &#61; [10&#xff0c; 20&#xff0c; 30&#xff0c; 40&#xff0c; 50]

import numpy as np
import pandas as pd
np_array &#61; np.array([10, 20, 30, 40, 50])
print("NumPy array:")
print(np_array)
new_series &#61; pd.Series(np_array)
print("Converted Pandas series:")
print(new_series)&#39;&#39;&#39;
NumPy array:
[10 20 30 40 50]
Converted Pandas series:
0 10
1 20
2 30
3 40
4 50
dtype: int32
&#39;&#39;&#39;


7、更改给定列或系列的数据类型为数值型

编写 Pandas 程序来更改给定列或系列的数据类型。

示例系列&#xff1a;
原始数据系列&#xff1a;
0 100
1 200
2 python
3 300.12
4 400
dtype&#xff1a; object
将所述数据类型更改为数字&#xff1a;
0 100.00
1 200.00
2 NaN
3 300.12
4 400.00
dtype&#xff1a; float64

import pandas as pd
s1 &#61; pd.Series([&#39;100&#39;, &#39;200&#39;, &#39;python&#39;, &#39;300.12&#39;, &#39;400&#39;])
print("Original Data Series:")
print(s1)
print("Change the said data type to numeric:")
s2 &#61; pd.to_numeric(s1, errors&#61;&#39;coerce&#39;)
print(s2)&#39;&#39;&#39;
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
Change the said data type to numeric:
0 100.00
1 200.00
2 NaN
3 300.12
4 400.00
dtype: float64
&#39;&#39;&#39;


to_numeric

这里使用了to_numeric()函数
【1】to_numeric()函数不能直接操作DataFrame对象

【2】to_numeric()函数较之astype()方法的优势在于解决了后者的局限性&#xff1a;只要待转换的数据中存在数字以外的字符&#xff0c;在使用后者进行类型转换时就会出现错误&#xff0c;而to_numeric()函数之所以可以解决这个问题&#xff0c;就源于其errors参数可以取值coerce——当出现非数字字符时&#xff0c;会将其替换为缺失值之后进行数据类型转换。

【3】参数
1、arg&#xff1a;表示要转换的数据&#xff0c;可以是list、tuple、Series
2、errors&#xff1a;错误采用的处理方式可以取值除raise、ignore外&#xff0c;还可以取值coerce&#xff0c;默认为raise。其中raise表示允许引发异常&#xff0c;ignore表示抑制异常&#xff0c;如果使用coerce&#xff0c;那么非数字元素就会被转化成NaN

8、将数据帧的第一列转换为series

编写一个 Pandas 程序&#xff0c;将数据帧的第一列转换为序列。

方法&#xff1a;直接使用切片iloc将数据取出&#xff0c;得到的结果就是series

import pandas as pd
d &#61; {&#39;col1&#39;: [1, 2, 3, 4, 7, 11], &#39;col2&#39;: [4, 5, 6, 9, 5, 0], &#39;col3&#39;: [7, 5, 8, 12, 1,11]}
df &#61; pd.DataFrame(data&#61;d)
print("Original DataFrame")
print(df)
s1 &#61; df.iloc[:,0] # 取全部数据的第0列
print("\n1st column as a Series:")
print(s1)
print(type(s1))&#39;&#39;&#39;
Original DataFramecol1 col2 col3
0 1 4 7
1 2 5 5
2 3 6 8
3 4 9 12
4 7 5 1
5 11 0 111st column as a Series:
0 1
1 2
2 3
3 4
4 7
5 11
Name: col1, dtype: int64

&#39;&#39;&#39;


9、将给定series转换为数组

编写一个 Pandas 程序&#xff0c;将给定的系列转换为数组。

import pandas as pd
import numpy as np
s1 &#61; pd.Series([&#39;100&#39;, &#39;200&#39;, &#39;python&#39;, &#39;300.12&#39;, &#39;400&#39;])
print("Original Data Series:")
print(s1)
print("Series to an array")
a &#61; np.array(s1)
print (a)&#39;&#39;&#39;
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
Series to an array
[&#39;100&#39; &#39;200&#39; &#39;python&#39; &#39;300.12&#39; &#39;400&#39;]
&#39;&#39;&#39;


10、将series列表转换为一个series

import pandas as pd
s &#61; pd.Series([[&#39;Red&#39;, &#39;Green&#39;, &#39;White&#39;],[&#39;Red&#39;, &#39;Black&#39;],[&#39;Yellow&#39;]])
print("Original Series of list")
print(s)
s &#61; s.apply(pd.Series).stack() # .reset_index(drop&#61;True)
print("删除原索引前&#xff1a;")
print(s)
s &#61; s.apply(pd.Series).stack().reset_index(drop&#61;True)
print("删除原索引后&#xff1a;")
print(s)&#39;&#39;&#39;
Original Series of list
0 [Red, Green, White]
1 [Red, Black]
2 [Yellow]
dtype: object
删除原索引前
0 0 Red1 Green2 White
1 0 Red1 Black
2 0 Yellow
dtype: object
删除原索引后
0 Red
1 Green
2 White
3 Red
4 Black
5 Yellow
dtype: object
&#39;&#39;&#39;

【1】np.stack()&#xff1a;对多个数组进行堆叠
【2】reset_index(drop&#61;True)&#xff1a;重新设置索引&#xff0c;并将原索引进行删除

11、对给定series进行排序

使用函数sort_values()

import pandas as pd
s &#61; pd.Series([&#39;100&#39;, &#39;200&#39;, &#39;python&#39;, &#39;300.12&#39;, &#39;400&#39;])
print("Original Data Series:")
print(s)
new_s &#61; s.sort_values()
print(new_s)&#39;&#39;&#39;
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
0 100
1 200
3 300.12
4 400
2 python
dtype: object
&#39;&#39;&#39;


12、向现有series添加一些数据

编写一个 Pandas 程序以将一些数据添加到现有series中。

import pandas as pd
s &#61; pd.Series([&#39;100&#39;, &#39;200&#39;, &#39;python&#39;, &#39;300.12&#39;, &#39;400&#39;])
print("Original Data Series:")
print(s)
print("\nData Series after adding some data:")
new_s &#61; s.append(pd.Series([&#39;500&#39;, &#39;php&#39;]))
print(new_s)&#39;&#39;&#39;
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: objectData Series after adding some data:
0 100
1 200
2 python
3 300.12
4 400
0 500
1 php
dtype: object
&#39;&#39;&#39;

【1】这里需要先将列表变成series&#xff0c;然后再使用append函数&#xff0c;否则直接用list会报错
【2】注意到index没有重新开始编号&#xff0c;而是在4之后直接继续使index从0开始
【3】如果想让index重新开始编号&#xff0c;则使用reset_index

a&#61;new_s.reset_index(drop&#61;True)
print(a)&#39;&#39;&#39;
0 100
1 200
2 python
3 300.12
4 400
5 500
6 php
dtype: object
&#39;&#39;&#39;


13、根据值和条件创建给定series的子集

import pandas as pd
s &#61; pd.Series([0, 1,2,3,4,5])
print("Original Data Series:")
print(s)
print("\nSubset of the above Data Series:")
new_s &#61; s[s < 3]
print(new_s)&#39;&#39;&#39;
Original Data Series:
0 0
1 1
2 2
3 3
4 4
5 5
dtype: int64Subset of the above Data Series:
0 0
1 1
2 2
dtype: int64
&#39;&#39;&#39;


14、更改给定series的索引顺序

编写一个 Pandas 程序来更改给定series的索引顺序。

import pandas as pd
s &#61; pd.Series(data &#61; [1,2,3,4,5], index &#61; [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;,&#39;D&#39;,&#39;E&#39;])
print("Original Data Series:")
print(s)
s &#61; s.reindex([&#39;B&#39;,&#39;A&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;])
print("Data Series after changing the order of index:")
print(s)&#39;&#39;&#39;
Original Data Series:
A 1
B 2
C 3
D 4
E 5
dtype: int64
Data Series after changing the order of index:
B 2
A 1
C 3
D 4
E 5
dtype: int64
&#39;&#39;&#39;

【1】reindex()是pandas对象的一个重要方法&#xff0c;其作用是创建一个新索引的新对象。

15、创建给定series的平均值和标准差

import pandas as pd
s &#61; pd.Series(data &#61; [1,2,3,4,5])
print("Original Data Series:")
print(s)
print("均值:")
print(s.mean())
print("标准差:")
print(s.std())
&#39;&#39;&#39;
Original Data Series:
0 1
1 2
2 3
3 4
4 5
dtype: int64
均值:
3.0
标准差:
1.5811388300841898
&#39;&#39;&#39;


16、计算给定series中每个唯一值的频率计数

编写一个 Pandas 程序来计算给定series的每个唯一值的频率计数。

import pandas as pd
import numpy as np
num_series &#61; pd.Series(np.take(list(&#39;0123456789&#39;), np.random.randint(10, size&#61;20)))
print("Original Series:")
print(num_series)
print("Frequency of each unique value of the said series.")
result &#61; num_series.value_counts()
print(result)&#39;&#39;&#39;
Original Series:
0 2
1 9
2 1
3 3
4 5
5 0
6 7
7 1
8 5
9 3
10 4
11 3
12 1
13 4
14 9
15 1
16 8
17 9
18 9
19 4
dtype: object
Frequency of each unique value of the said series.
9 4
1 4
3 3
4 3
5 2
2 1
0 1
7 1
8 1
dtype: int64
&#39;&#39;&#39;

【1】np.take&#xff1a;检索a对应的行&#xff0c;并返回索引行的值。具体用法&#xff0c;请看》》np.take
【2】value_counts() &#xff1a;返回一个序列 Series&#xff0c;该序列包含每个值的数量。也就是说&#xff0c;对于数据框中的任何列&#xff0c;value-counts () 方法会返回该列每个项的计数。
【3】这题也不是非要使用np.take&#xff0c;使用下面这段代码也是能够得出相同的结果的

import pandas as pd
import numpy as np
num_series &#61; pd.Series(np.random.randint(10, size&#61;20),list(&#39;0123456789&#39;)*2)
print("Original Series:")
print(num_series)
print("Frequency of each unique value of the said series.")
result &#61; num_series.value_counts()
print(result)&#39;&#39;&#39;
Original Series:
0 1
1 9
2 1
3 0
4 7
5 9
6 2
7 0
8 8
9 8
0 2
1 2
2 2
3 5
4 6
5 6
6 9
7 4
8 6
9 6
dtype: int32
Frequency of each unique value of the said series.
2 4
6 4
9 3
1 2
0 2
8 2
7 1
5 1
4 1
dtype: int64
&#39;&#39;&#39;


17、将单词的第一个和最后一个字符转为大写

将每个单词的第一个和最后一个字符转换为大写

关键&#xff1a;使用map和匿名函数&#xff0c;map的作用是将匿名函数作用于series当中的每个单词上面&#xff0c;而匿名函数则使用切片的方式&#xff0c;加好可以直接连接字符串

import pandas as pd
series1 &#61; pd.Series([&#39;php&#39;, &#39;python&#39;, &#39;java&#39;, &#39;c#&#39;])
print("Original Series:")
print(series1)
result &#61; series1.map(lambda x: x[0].upper() &#43; x[1:-1] &#43; x[-1].upper())
print("\nFirst and last character of each word to upper case:")
print(result)

在这里插入图片描述

18、计算给定系列中每个单词的字符数

分析&#xff1a;和上面那题差不多&#xff0c;len可以求每个单词的长度&#xff08;字符数&#xff09;

import pandas as pd
series1 &#61; pd.Series([&#39;Php&#39;, &#39;Python&#39;, &#39;Java&#39;, &#39;C#&#39;])
print("Original Series:")
print(series1)
result &#61; series1.map(lambda x: len(x))
print("\nNumber of characters in each word in the said series:")
print(result)

19、将一系列日期字符串转换为时间序列

关键&#xff1a;pd.to_datetime(date_series)

import pandas as pd
date_series &#61; pd.Series([&#39;01 Jan 2015&#39;, &#39;10-02-2016&#39;, &#39;20180307&#39;, &#39;2014/05/06&#39;, &#39;2016-04-12&#39;, &#39;2019-04-06T11:20&#39;])
print("Original Series:")
print(date_series)
print("\nSeries of date strings to a timeseries:")
print(pd.to_datetime(date_series))

在这里插入图片描述

20、垂直和水平堆叠两个给定系列

编写一个Pandas程序&#xff0c;在垂直和水平方向上堆叠两个给定的系列。

import pandas as pd
series1 &#61; pd.Series(range(10))
series2 &#61; pd.Series(list(&#39;pqrstuvwxy&#39;))
print("原始的两个列表")
print(series1)
print("\n")
print(series2)
series1.append(series2)
print("\n")
print(series1.append(series2))
df&#61;pd.concat([series1, series2], axis&#61;1)
print("\n")
print(df)
df&#61;pd.concat([series1, series2], axis&#61;0)
print("\n")
print(df)

在这里插入图片描述
注意到index是直接拼接&#xff0c;没有改变
在这里插入图片描述
在这里插入图片描述
如果上面的axis&#61;1改成axis&#61;0&#xff0c;则和append是一样的


推荐阅读
author-avatar
我很丑但我可以很温柔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有