欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pandas如何使用列表和字典创建 Series

程序员文章站 2022-03-02 09:57:30
目录01 使用列表创建 series02 使用 name 参数创建 series03 使用简写的列表创建 series04 使用字典创建 series05 如何使用 numpy 函数创建 series...

前言:

pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

为了让大家对pandas的操作更加熟练,我整理了一些关于pandas的小操作,会依次为大家展示

今天我将先为大家如何关于pandas如何使用列表和字典创建 series

01 使用列表创建 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

02 使用 name 参数创建 series

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

03 使用简写的列表创建 series

import pandas as pd
 
ser3 = pd.series(["a"]*4)
print(ser3)


output:

0    a
1    a
2    a
3    a
dtype: object

04 使用字典创建 series

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

05 如何使用 numpy 函数创建 series

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

06 如何获取 series 的索引和值

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 ]

07 如何在创建 series 时指定索引

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

08 如何获取 series 的大小和形状

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

09 如何获取 series 开始或末尾几行数据

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

10 使用切片获取 series 子集

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

到此这篇关于pandas如何使用列表和字典创建 series的文章就介绍到这了,更多相关pandas使用列表和字典创建 series内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!