2015-10-15 32 views
0
改變字符串二進制列

代碼:我如何能在大熊貓

import pandas as pd 
df = pd.DataFrame(columns=['home_team', 'away_team']) 
df = df.append(pd.Series(['a', 'b'], index=['home_team', 'away_team']), ignore_index=True) 
df = df.append(pd.Series(['d', 'c'], index=['home_team', 'away_team']), ignore_index=True) 
df = df.append(pd.Series(['c', 'd'], index=['home_team', 'away_team']), ignore_index=True) 
df = df.append(pd.Series(['b', 'a'], index=['home_team', 'away_team']), ignore_index=True) 
print(df) 

原始數據幀:

home_team away_team 
0 a b 
1 d c 
2 c d 
3 b a 

我想將它轉換爲:

bit0 bit1 bit2 bit3 
0 0 0 0 1 
1 1 1 1 0 
2 1 0 1 1 
3 0 1 0 0 

""" 
a:00 
b:01 
c:10 
d:11 
""" 

回答

0
import string 
alpha = string.ascii_lowercase 

dic_alpha = {ltr: alpha.index(ltr) for ltr in alpha } 

to_bin = lambda i: '{0:05b}'.format(i) 
dic_alpha_bin = {key : list(to_bin(val)) for key,val in dic_alpha.iteritems()} 

lst_c1 = ['bit0','bit1','bit2','bit3','bit4'] 
lst_c2 = ['bit5','bit6','bit7','bit8','bit9'] 

df[lst_c1] = df['home_team'].apply(lambda x: pd.Series(dic_alpha_bin[x])) 
df[lst_c2] = df['away_team'].apply(lambda x: pd.Series(dic_alpha_bin[x])) 
+0

a,b,c,d是隊名,例如'Real Madrid'\t,'La Coruna','Zaragoza',\t'特內里費'。我想在一列中找到一點。 – also

+0

@also你應該如何解析你自己的方式.. –

+0

我明白了。謝謝! – also