嗨,我有一个大熊猫数据帧,其中包含以下信息:
5%
4
2.1
4.000
0.100%BuyType:2%,SellType:29%
销售类型24%,费用类型0.2%,非费用2%
购买类型:无销售代理3购买代理4…
也就是说,本专栏为我提供了一些需要提取的百分比信息。
前5行很容易理解,即使有时有%,有时没有。
最后三个比较棘手,但是总是有序列
SOME CODE_ID - SOME NUMBER
有没有办法在熊猫身上提取这些信息?假设将不同的百分比存储在相应的变量中(此处BuyType在第6行等于2,在最后一行等于NONE,等等)。
也许一种策略是获得CODE_ID (non numeric)
的完整集合,然后将其传递给某个正则表达式函数。我不知道该怎么做。
输出应该是:
one buytype selltype feetype nonfee sellagent buyagent
5 0 0 0 0 0
4 0 0 0 0 0
2.1 0 0 0 0 0
4 0 0 0 0 0
0.1 0 0 0 0 0
0 2 29 0 0 0
0 0 0 24 0.2 2 0
0 NONE 0 0 0 3 4
欢迎任何建议非常感谢!!!
您可以尝试这个解决方案,但它非常复杂,并且在数据中不能是char |
,因为我使用它进行拆分:
import pandas as pd
import numpy as np
import io
import re
temp=u"""5%
4
2.1
4.000
0.100% BuyType: 2%,SellType: 29%
SellType 24%, fee type 0.2%, Non-fee 2%
BuyType: NONE Sell Agent 3 buy agent 4"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep="|", header=None, names=['s'])
print df
def f(x):
return re.sub(r"([d.]+|NONE)%?", r"1|", ''.join(x)).rstrip('|')
#function - info http://stackoverflow.com/q/34857379/2901002
df['s1'] = df.apply(f, axis=1)
#remove , : - arbitary whitespace(s+)
df['s1'] = df['s1'].str.replace(r'[-,:s+]', '').str.lower()
print df
s s1
0 5% 5
1 4 4
2 2.1 2.1
3 4.000 4.000
4 0.100% BuyType: 2%,SellType: 29% 0.100|buytype2|selltype29
5 SellType 24%, fee type 0.2%, Non-fee 2% selltype24|feetype0.2|nonfee2
6 BuyType: NONE Sell Agent 3 buy agent 4 buytypenone|sellagent3|buyagent4
#split data by | to new df
df = pd.DataFrame([ x.split('|') for x in df['s1'].tolist() ])
#stack data to 2 columns
df = df.stack().reset_index(level=0)
df.columns = ['id','data']
#extract number
df['number'] = df['data'].str.replace(r'[a-z]', '')
#extract none
df.loc[df.data.str[-4:].str.contains('none'), 'number'] = 'NONE'
#extract text
df['text'] = df['data'].str.replace(r'd*.d+|d+', '')
#add text to empty values of column text
df.loc[df['text'].str.len() == 0, 'text'] = 'one'
#remove none
df.loc[df.data.str[-4:].str.contains('none'), 'text'] =
df.loc[df.data.str[-4:].str.contains('none'), 'text'].str[:-4]
print df
id data number text
0 0 5 5 one
0 1 4 4 one
0 2 2.1 2.1 one
0 3 4.000 4.000 one
0 4 0.100 0.100 one
1 4 buytype2 2 buytype
2 4 selltype29 29 selltype
0 5 selltype24 24 selltype
1 5 feetype0.2 0.2 feetype
2 5 nonfee2 2 nonfee
0 6 buytypenone NONE buytype
1 6 sellagent3 3 sellagent
2 6 buyagent4 4 buyagent
print df.pivot(index='id', columns='text', values='number').fillna(0)
text buyagent buytype feetype nonfee one sellagent selltype
id
0 0 0 0 0 5 0 0
1 0 0 0 0 4 0 0
2 0 0 0 0 2.1 0 0
3 0 0 0 0 4.000 0 0
4 0 2 0 0 0.100 0 29
5 0 0 0.2 2 0 0 24
6 4 NONE 0 0 0 3 0