2016-04-08 8 views

答えて

2

あなたはSeriesを構築し、その後、文字のリストを作成するliststrにタイプをキャストしてapplyできるので、それはあなたが新しい列として戻って追加することができDFを返します。

In [13]: 
df[['b','c','d','e']] = df['b'].astype(str).apply(lambda x: pd.Series(list(x))).astype(int) 
df 

Out[13]: 
    a b c d e 
0 foo 2 3 4 8 
1 bar 6 1 3 2 
2 baz 5 8 4 3 
0

indexing with strを使用できます。

#convert column b to string 
df['b'] = df.b.astype(str) 

#indexing with str 
df['c'] = df.b.str[1] 
df['d'] = df.b.str[2] 
df['e'] = df.b.str[3] 
df['b'] = df.b.str[0] 

#if need convert columns to int 
df[['b','c','d','e']] = df[['b','c','d','e']].astype(int) 
print df 
    a b c d e 
0 foo 2 3 4 8 
1 bar 6 1 3 2 
2 baz 5 8 4 3 
関連する問題