name
参数允许您为 Series
对象赋予一个名称,也就是列名。这样当您将其放入 DataFrame
中时,该列将根据 name
参数命名。
例如:
In [1]: s = pd.Series(["A","B","C"], name="foo")
In [2]: s
Out[2]:
0 A
1 B
2 C
Name: foo, dtype: object
In [3]: pd.DataFrame(s)
Out[4]:
foo
0 A
1 B
2 C
如果你没有给你的Series
一个name
,它将会自动被命名。在这里它将成为dataframe
对象中的0
:
0
0 A
1 B
2 C
对于fastpath
,它是一个内部参数,并且已经有一个问题报告:
'name'参数还有另一种用法。我来举个例子。在这个例子中,我们将看到参数'name'可以用作值的索引名称。
purchase_1 = pd.Series({'Name': 'JJ',
'Item': 'A',
'Cost': 22.00})
purchase_2 = pd.Series({'Name': 'KK',
'Item': 'B',
'Cost': 22.50})
dfn = pd.DataFrame([purchase_1, purchase_2], index=['Store X', 'Store Y'])
dfn = dfn.append(pd.Series(data={'Cost': 30.00, 'Item': 'C','Name': 'TT'}, name='Store Y'))
dfn
Out[3]:
Cost Item Name
Store X 22.0 A JJ
Store Y 22.5 B KK
Store Y 30.0 C TT
当你将它们拼接在一起时,pd.Series
的name
变成了column name
。反之亦然,当你从dataframe
中提取列时,该列的name
成为提取的pd.Series
的名称。
import pandas as pd
import numpy as np
s1 = pd.Series(np.random.randn(10), name='series1')
s2 = pd.Series(np.random.randn(10), name='series2')
pd.concat([s1, s2], axis=1)
Out[16]:
series1 series2
0 0.3499 0.3017
1 -2.2980 -1.1121
2 -1.4517 -0.5107
3 -0.4596 -0.0855
4 -0.3230 0.5391
5 -0.1764 -0.3218
6 2.4806 -0.6129
7 1.6766 1.1408
8 -1.2407 0.4857
9 0.3587 -1.5752
df.ix[0]
从一个DataFrame
中切片一行,那么得到的对象是一个Series
,其原始索引作为name
属性。至于fastpath
,我还没有遇到过。 - Finwoodname
是 Series 的属性,在 df 中变得更加重要,因为name
是列的名称。我无法确定fastpath
的用途,但查看源代码后发现它是一个内部标志,用于测试数据是否为同质数组,以便在某些构造过程中进行短路处理。 - EdChum