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
What is the name parameter in Pandas Series?
提问by Sounak
采纳答案by jrjc
The name
argument allows you to give a name to a Series
object, i.e. to the column. So that when you'll put that in a DataFrame
, the column will be named according to the name
parameter.
该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 name
to your Series
it will be named automatically. Here it will be a 0
in the dataframe
object:
如果你不给name
你的Series
它会自动命名。这将是一个0
在dataframe
对象:
0
0 A
1 B
2 C
For the fastpath
, it's an internal parameter and an issue has already been reported :
对于fastpath
,它是一个内部参数,并且已经报告了一个问题:
回答by Jianxun Li
The name
of pd.Series
become the column name
when you concat them together. Vice versa, when you extract a column from dataframe
, it has the column name
as the name of the extracted pd.Series
.
的name
的pd.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