我有以下数据
Description
4 GB+ 2 GB Night 3G/2G Data
Unlimited Local & STD Calls + 500 MB 3G/2G Data (T&C apply)
9GB + 8GB night data+ 6GB next night data
8 GB data 4G
我想要的是提取数据量(4GB 等)并将它们合并到一个列中
df2=df['Description'].str.extract('([0-9]+(\.[0-9][0-9]?)?\s?GB|[0-9]+(\.[0-9][0-9]?)?\s?MB)')
我用熊猫的功能extractall()
太多,但两者extract
并extractall()
给我造成这样的
0 1 2
4GB Nan Nan #2 gb is missing
500MB Nan Nan
9GB Nan Nan # 8gb 6 gb is missing
8Gb Nan Nan
我错在哪里?此外,当将行与df.fillna(' ')
我组合时,我会得到这样的输出
0
4GB,2GB,
500MB, ,
9GB,8GB,6GB
8GB, ,
虽然我想要的是
0
4GB,2GB
500MB
9GB,8GB,6GB
8GB
我不想要空格。在 Pandas 中有什么方法可以获取上述格式的数据吗?我是python的初学者,不知道如何实现。如果有其他方法请提及。
编辑:
这是完整的代码:
df2=df['Description'].str.extractall('([0-9]+(\.[0-9][0-9]?)?\s?GB|[0- 9]+(\.[0-9][0-9]?)?\s?MB)')
#print df2
df2[1].fillna("",inplace=True);
df2[2].fillna("",inplace=True)
print df2
df3=df2[0]+','+df2[1]+','+df2[2];
print df3
使用extractall 应该像下面这样工作:
df.Description.str\
.extractall('(\d*\s?[GM]B)').groupby(level=0)\
.apply(lambda x: ','.join(x[0])\
.replace(' ',''))
Out[75]:
0 4GB,2GB
1 500MB
2 9GB,8GB,6GB
3 8GB
dtype: object
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句