📜  从数组创建熊猫系列

📅  最后修改于: 2022-05-13 01:55:41.691000             🧑  作者: Mango

从数组创建熊猫系列

Pandas Series是一个一维标记数组,能够保存任何数据类型(整数、字符串、浮点数、 Python对象等)。必须记住,与Python列表不同,Series 将始终包含相同类型的数据。

让我们看看如何从数组中创建 Pandas 系列。

方法#1:从没有索引的数组创建一个系列。

在这种情况下,由于没有传递索引,因此默认情况下索引将是range(n) ,其中n是数组长度。

# importing Pandas & numpy
import pandas as pd
import numpy as np
  
# numpy array
data = np.array(['a', 'b', 'c', 'd', 'e'])
  
# creating series
s = pd.Series(data)
print(s)
输出:
0    a
1    b
2    c
3    d
4    e
dtype: object


方法#2:从具有索引的数组创建一个系列。

在这种情况下,我们将 index 作为参数传递给构造函数。

# importing Pandas & numpy
import pandas as pd
import numpy as np
  
# numpy array
data = np.array(['a', 'b', 'c', 'd', 'e'])
  
# creating series
s = pd.Series(data, index =[1000, 1001, 1002, 1003, 1004])
print(s)
输出:
1000    a
1001    b
1002    c
1003    d
1004    e
dtype: object