从数组中创建一个潘达系列
Pandas Series是一个一维的标记数组,能够容纳任何数据类型 (整数、字符串、浮点数、Python 对象,等等)。必须记住,与Python列表不同,一个系列总是包含相同类型的数据。
让我们看看如何从数组中创建一个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
方法二:从数组中创建一个带有索引的系列。
在这种情况下,我们将把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