Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫系列中的名称参数是什么?_Python_Numpy_Pandas - Fatal编程技术网

Python 熊猫系列中的名称参数是什么?

Python 熊猫系列中的名称参数是什么?,python,numpy,pandas,Python,Numpy,Pandas,在的文档中,name和fastpath的使用参数没有解释。它们是做什么的?将pd.Series的名称组合在一起时,将成为列名。反之亦然,当您从dataframe提取列时,它将列名作为提取的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')

在的文档中,
name
fastpath
的使用参数没有解释。它们是做什么的?

pd.Series
名称
组合在一起时,将成为
列名
。反之亦然,当您从
dataframe
提取列时,它将
列名
作为提取的
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

name
参数允许您为
Series
对象(即列)指定名称。因此,当您将其放入
数据帧
中时,该列将根据
名称
参数命名

例如:

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
如果您没有为您的
系列提供
名称
,它将自动命名。这里它将是
dataframe
对象中的
0

   0
0  A
1  B
2  C
对于
fastpath
,它是一个内部参数,已经报告了一个问题:


添加到所有信息中,我刚刚了解到,当您必须使用Series附加行时,有两个选项: 1) ignore_index=True,在这种情况下,它将删除设置的索引并将其重置为数字索引(默认值)
2) 使用名称有助于保持当前数据帧结构,并使用名称参数作为多索引(按顺序)

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

如果使用
df.ix[0]
数据帧
切片一行,则生成的对象是一个
系列
,原始索引为
名称
属性
fastpath
我还没有遇到过。@Finwood你能举个例子吗?
name
是这个系列的一个属性,它在一个df中变得更重要,
name
是列的名称。我不能肯定地说什么是
fastpath
,但看看源代码,它是一个内部标志,用来测试数据是否是一个同质数组来短路一些结构。虽然我理解它的用法,但我总是发现pandas在这里错误地混合了概念。我理解他们支持单索引和多索引数据帧的df.append(pd.Series)的基本原理,但他们不应该引入另一个属性吗?在我看来,如果多索引数据帧的代码将属性拆分为name_index、name_value/index_value,那么它们会更干净。例如,在您描述的案例中:,如果您将数据框的索引命名为“store\u idx”,则该系列的属性应为:name\u index=“store\u idx”,name\u value/index\u value=“store Y”