创建具有所需长度的pd.Series对象

2024-10-03 17:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

sampleA = pd.concat([orderByUsersA['orders'],pd.Series(0,index=np.arange(visitors[visitors['group']=='A']['visitors'].sum() - len(orderByUsersA['orders'])), name='orders')],axis=0)

嗨, 这个问题是我课堂上讲的

我的问题是当串联时,在级数构造的第二个参数中,(0,)Zero代表什么。我试图通过删除零来检查它的功能,它返回了一个未来的警告

DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning. """Entry point for launching an IPython kernel.

参数0的作用是什么?可以给出哪些其他参数


Tags: for参数indexnpgroupseriespdsum
1条回答
网友
1楼 · 发布于 2024-10-03 17:18:26

我将简单介绍np.arange中的部分,但本质上您可以使用以下代码创建一个系列

import pandas as pd
import numpy as np

pd.Series(0, index=np.arange(10), name='orders')

创建这样的系列使用了位置(0)和命名(index=np.arange(10)name='orders')参数的组合

如果我们看一下Series class,它是一个具有多个参数的类,并且所有参数都定义了默认值:

class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

因此,按照创建对象的方式,0按位置分配给第一个预期参数,即data,然后根据指定参数的名称确定indexname是什么,最后,它为未指定的参数使用提供的默认值

所以你的对象是用

pd.Series(0, index=np.arange(10), name='orders')

等同于完全显式创建,包括:

pd.Series(data=0, index=np.arange(10), name='orders', dtype=None,
          copy=False, fastpath=False)

通常情况下,您不会写出所有默认值,只有在参数偏离默认值、是没有默认值的参数或指定默认值有助于代码自我记录时,才指定参数

相关问题 更多 >