除了直接创建 Series 外,我们还可以通过字典创建 Series,同时设置名称。例如,以下代码创建了一个以城市名称为索引的 Series 并定义名称为"城市人口": city_population=pd.Series({'北京':2154,'上海':2424,'广州':1490,'深圳':1340})city_population.name="城市人口"print(city_population) 1. 2. 3. 4. 5...
s = pd.Series(25,index=['a','b','c']) s 1. 2. a 25 b 25 c 25 dtype: int64 1. 2. 3. 4. 此时为了创建一个Series类型,必须要增加index,用以告诉Series几个元素构成,标签是多少。 c = pd.Series({'a':9,'b':8,'c':7,'d':6}) c 1. 2. a 9 b 8 c 7 d 6 dtype: i...
Series的name属性 Series 切片 类似于NumPy, 可以通过索引切片选取或处理Series中的一个或多个值,其返回的结果依然是Series类型的对象。 import pandas as pd import numpy as np s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e']) print(s) # 显示Series中的元素 print(s[...
Series.name:Return name of the Series. 4 索引 Series.loc:Access a group of rows and columns by label(s) or a boolean array. Series.iloc:Purely integer-location based indexing for selection by position. 5 计算、描述性统计 Series.value_counts:Return a Series containing counts of unique values...
Series 支持name 属性: In [33]: s = pd.Series(np.random.randn(5), name='something') In [34]: s Out[34]: 0 1.926419 1 1.175251 2 -0.568534 3 -0.014069 4 1.401082 Name: something, dtype: float64 In [35]: s.name Out[35]: 'something' 一般情况下,Series 自动分配 name 属性,特别...
import pandas as pd import numpy as np • s = {"ton": 20, "mary": 18, "jack": 19, "car": None} • sa = pd.Series(s, name="age") • print(sa) [/code] * 打印 ```code car NaN jack 19.0 mary 18.0 ...
s=pd.Series(dic)print(s,type(s))#运行结果1hello2python3 [1, 2] a1b2c3dtype: object<class'pandas.core.series.Series'> 2、通过数组(ndarray)创建 参数index:是Series的标签 参数name: 是Series的名称,没有的话默认为None。可以用rename()来更改,更改后生成新的Series,不改变原来的Series ...
df=pd.DataFrame({"name":["张三","李四","朱五"],"date":[datetime(2022,2,21),datetime(2022,2,22),datetime(2022,2,23)]})df 输出如下图所示: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 df["week_num1"]=df["date"].dt.dayofweek ...
df=pd.DataFrame({'key1':['a','a','b','b','a'],'key2':['one','two','one','two','one'], 'data1':np.random.randn(5),'data2':np.random.randn(5)}) df Python 复制代码 9 1 2 gg=df.groupby(df['key1']) ...
import pandas as pd if __name__ == "__main__": s = pd.Series() print(s) # output: # Series([], dtype: float64) (2)使用ndarray创建Series 使用ndarray作为数据时,传递的索引必须与ndarray具有相同的长度。 如果没有传递索引值,那么默认的索引是range(n),其中n是数组长度,即[0,1,2,3…....