assign split后为啥多一层pandas.core.series.Series呢

刘超 13天前 ⋅ 274 阅读   编辑

一、描述

  pandas0.24.2 assign split后为啥多一层pandas.core.series.Series,如下

>>> import pandas as pd
>>> data1 = {'day':["20201011"], 'CreativeID':["buyer:cid:adv3636360199680:0801680316938880002"]}
>>> 
>>> df=pd.DataFrame(data1)
>>> 
>>> dfa = df.assign(advertiserId=lambda x: type(x['CreativeID'].str.split(':')))
>>> dfa
                                       CreativeID       day                         advertiserId
0  buyer:cid:adv3636360199680:0801680316938880002  20201011  <class 'pandas.core.series.Series'>
>>> dfa = df.assign(advertiserId=lambda x: type(x['CreativeID'].str.split(':')[0]))
>>> dfa
                                       CreativeID       day   advertiserId
0  buyer:cid:adv3636360199680:0801680316938880002  20201011  <type 'list'>
>>> dfa = df.assign(advertiserId=lambda x: x['CreativeID'].str.split(':')[0][2])
>>> dfa
                                       CreativeID       day      advertiserId
0  buyer:cid:adv3636360199680:0801680316938880002  20201011  adv3636360199680

二、解决方法

  series,只是一个一维数据结构,它由index和value组成。
  dataframe,是一个二维结构,除了拥有index和value之外,还拥有column。

  联系:dataframe由多个series组成,无论是行还是列,单独拆分出来都是一个series。

三、参考文章
1、https://blog.csdn.net/missyougoon/article/details/83301712


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: