Python Pandas系列中的name参数是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31292140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:46:47  来源:igfitidea点击:

What is the name parameter in Pandas Series?

pythonnumpypandas

提问by Sounak

In the doc of Series, the use parameter of nameand fastpathis not explained. What do they do?

在的文档系列中,使用参数namefastpath没有解释。他们在做什么?

采纳答案by jrjc

The nameargument allows you to give a name to a Seriesobject, i.e. to the column. So that when you'll put that in a DataFrame, the column will be named according to the nameparameter.

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

example:

例子:

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

If you don't give a nameto your Seriesit will be named automatically. Here it will be a 0in the dataframeobject:

如果你不给name你的Series它会自动命名。这将是一个0dataframe对象:

   0
0  A
1  B
2  C

For the fastpath, it's an internal parameter and an issue has already been reported :

对于fastpath,它是一个内部参数,并且已经报告了一个问题:

https://github.com/pydata/pandas/issues/6903

https://github.com/pydata/pandas/issues/6903

回答by Jianxun Li

The nameof pd.Seriesbecome the column namewhen you concat them together. Vice versa, when you extract a column from dataframe, it has the column nameas the name of the extracted pd.Series.

namepd.Series成为column name当你Concat的在一起。反之亦然,当您从 中提取一列时dataframe,它column 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

回答by Ajay Shah

Adding to all the information, I just learned that when you have to append a row using Series, two options are there: 1) ignore_index = True, in this case, it will remove the set index and reset it to numeric index (default) 2) using the name helps to keep the current dataframe structure and use the name parameter as the multi index (in order).

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

回答by cyber-math

There is another usage of the 'name' parameter. I will give an example. In this example we will see that the parameter 'name' could be used as an index name for values.

'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