pandas用列表和字典创建Series的代码是什么
Admin 2022-05-28 群英技术资讯 1480 次浏览
这篇文章主要讲解了“pandas用列表和字典创建Series的代码是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“pandas用列表和字典创建Series的代码是什么”吧!Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
为了让大家对pandas的操作更加熟练,我整理了一些关于pandas的小操作,会依次为大家展示
今天我将先为大家如何关于pandas如何使用列表和字典创建 Series。
import pandas as pd ser1 = pd.Series([1.5, 2.5, 3, 4.5, 5.0, 6]) print(ser1)
Output:
0 1.5
1 2.5
2 3.0
3 4.5
4 5.0
5 6.0
dtype: float64
import pandas as pd ser2 = pd.Series(["India", "Canada", "Germany"], name="Countries") print(ser2)
Output:
0 India
1 Canada
2 Germany
Name: Countries, dtype: object
import pandas as pd ser3 = pd.Series(["A"]*4) print(ser3)
Output:
0 A
1 A
2 A
3 A
dtype: object
import pandas as pd
ser4 = pd.Series({"India": "New Delhi",
"Japan": "Tokyo",
"UK": "London"})
print(ser4)
Output:
India New Delhi
Japan Tokyo
UK London
dtype: object
import pandas as pd import numpy as np ser1 = pd.Series(np.linspace(1, 10, 5)) print(ser1) ser2 = pd.Series(np.random.normal(size=5)) print(ser2)
Output:
0 1.00
1 3.25
2 5.50
3 7.75
4 10.00
dtype: float64
0 -1.694452
1 -1.570006
2 1.713794
3 0.338292
4 0.803511
dtype: float64
import pandas as pd
import numpy as np
ser1 = pd.Series({"India": "New Delhi",
"Japan": "Tokyo",
"UK": "London"})
print(ser1.values)
print(ser1.index)
print("\n")
ser2 = pd.Series(np.random.normal(size=5))
print(ser2.index)
print(ser2.values)
Output:
['New Delhi' 'Tokyo' 'London']
Index(['India', 'Japan', 'UK'], dtype='object')
RangeIndex(start=0, stop=5, step=1)
[ 0.66265478 -0.72222211 0.3608642 1.40955436 1.3096732 ]
import pandas as pd
values = ["India", "Canada", "Australia",
"Japan", "Germany", "France"]
code = ["IND", "CAN", "AUS", "JAP", "GER", "FRA"]
ser1 = pd.Series(values, index=code)
print(ser1)
Output:
IND India
CAN Canada
AUS Australia
JAP Japan
GER Germany
FRA France
dtype: object
import pandas as pd
values = ["India", "Canada", "Australia",
"Japan", "Germany", "France"]
code = ["IND", "CAN", "AUS", "JAP", "GER", "FRA"]
ser1 = pd.Series(values, index=code)
print(len(ser1))
print(ser1.shape)
print(ser1.size)
Output:
6
(6,)
6
Head()函数:
import pandas as pd
values = ["India", "Canada", "Australia",
"Japan", "Germany", "France"]
code = ["IND", "CAN", "AUS", "JAP", "GER", "FRA"]
ser1 = pd.Series(values, index=code)
print("-----Head()-----")
print(ser1.head())
print("\n\n-----Head(2)-----")
print(ser1.head(2))
Output:
-----Head()-----
IND India
CAN Canada
AUS Australia
JAP Japan
GER Germany
dtype: object
-----Head(2)-----
IND India
CAN Canada
dtype: object
Tail()函数:
import pandas as pd
values = ["India", "Canada", "Australia",
"Japan", "Germany", "France"]
code = ["IND", "CAN", "AUS", "JAP", "GER", "FRA"]
ser1 = pd.Series(values, index=code)
print("-----Tail()-----")
print(ser1.tail())
print("\n\n-----Tail(2)-----")
print(ser1.tail(2))
Output:
-----Tail()-----
CAN Canada
AUS Australia
JAP Japan
GER Germany
FRA France
dtype: object
-----Tail(2)-----
GER Germany
FRA France
dtype: object
Take()函数:
import pandas as pd
values = ["India", "Canada", "Australia",
"Japan", "Germany", "France"]
code = ["IND", "CAN", "AUS", "JAP", "GER", "FRA"]
ser1 = pd.Series(values, index=code)
print("-----Take()-----")
print(ser1.take([2, 4, 5]))
Output:
-----Take()-----
AUS Australia
GER Germany
FRA France
dtype: object
import pandas as pd
num = [000, 100, 200, 300, 400, 500, 600, 700, 800, 900]
idx = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
series = pd.Series(num, index=idx)
print("\n [2:2] \n")
print(series[2:4])
print("\n [1:6:2] \n")
print(series[1:6:2])
print("\n [:6] \n")
print(series[:6])
print("\n [4:] \n")
print(series[4:])
print("\n [:4:2] \n")
print(series[:4:2])
print("\n [4::2] \n")
print(series[4::2])
print("\n [::-1] \n")
print(series[::-1])
Output:
[2:2]
C 200
D 300
dtype: int64
[1:6:2]
B 100
D 300
F 500
dtype: int64
[:6]
A 0
B 100
C 200
D 300
E 400
F 500
dtype: int64
[4:]
E 400
F 500
G 600
H 700
I 800
J 900
dtype: int64
[:4:2]
A 0
C 200
dtype: int64
[4::2]
E 400
G 600
I 800
dtype: int64
[::-1]
J 900
I 800
H 700
G 600
F 500
E 400
D 300
C 200
B 100
A 0
dtype: int64
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
内容介绍导语013Dplot1.基本语法2.PythonCmd3.举例02绘制Scatter03绘制3DSurface导语很多情况下,为了能够观察到数据之间的内部的关系,可以使用绘图来更好的显示规律。
这篇文章主要为大家介绍了Python Pyecharts绘制桑基图分析用户行为路径,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
今天看了open函数,看到w+ r+ a+ 这种可读可写的操作,下面这篇文章主要给大家介绍了关于python使用open函数对文件进行处理的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
这篇文章主要介绍了python等间距取值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
本文主要介绍了Python曲线平滑的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008