分裂数组中的字符串 - python

时间:2015-11-11 15:16:49

标签: python pandas

我有一个带有数组变量的pandas数据框,该数组当前由两部分字符串组成,如下例所示。第一部分是日期时间,第二部分是价格。数据框中的记录具有不同长度的price_trend数组。

instanceof

我想将数组中的每个字符串拆分为冒号(:)周围的两部分,但是当我运行下面的代码时,price_trend中的所有值都被替换为nan

Id  Name    Color    price_trend                
1   apple   red      '1420848000:1.25', '1440201600:1.35', '1443830400:1.52'                 60
2   lemon   yellow   '1403740800:0.32','1422057600:0.25'

我想将数组保留在此数据框中,而不是创建新数组。

2 个答案:

答案 0 :(得分:0)

select a.*, b.DataCutId as UpdDataCutID, b.Name as UpdName, b.Surname as UpdSurname, b.City as UpdCity, b.PostCode as UpdPostCode, b.HomePhone as UpdHomePhone from Customer a inner join Customer b on a.CustomerId = b.CustomerId and a.Id < b.ID and (a.Name <> b.Name OR a.Surname <> b.Surname OR a.City <> b.City OR a.PostCode <> b.PostCode OR a.HomePhone <> b.HomePhone)

df['price_trend'].apply(lambda x:[i.split(':') for i in x])

答案 1 :(得分:0)

我认为以下代码应该适合你

>>> df={}
>>> df['p']=['1420848000:1.25', '1440201600:1.35', '1443830400:1.52']
>>> df['p']=[ x.split(':') for x in df['p']]
>>> df
{'p': [['1420848000', '1.25'], ['1440201600', '1.35'], ['1443830400', '1.52']]}