2015-12-15 21 views
1

我有3列(包括指數)的數據幀:串聯指數和字符串到新列

name age 
0 satya 24 
1 abc  26 
2 xyz  29 
3 def  32 

所以需要添加一個新列detail這將細節文件名和值存儲在該列應該像(str(file_index no))

name age detail 
0 satya 24 file_0 
1 abc  26 file_1 
2 xyz  29 file_2 
3 def  32 file_3 

實現,我嘗試了以下

df['detail']= str('file_'+df.index) #not working shows error 
df['detail'] = str('file'+'_'+str(df.index)) #worked but not what i want 
df['detail'] = str(s+'_'+df.index[0].astype(str)) #error 

for loop and the terrowrows

for index, row in df.iterrows(): 
     df['detail'] = str('file'+'_'+row[index]) #IndexError: index out of bounds 

for index, row in df.iterrows(): 
df['idx'] = str(s+'_'+df.index[row].astype(str)) ###IndexError: arrays used as indices must be of integer (or boolean) type 

所以請建議。

回答

1

您可以使用astypeindex

df['detail']= 'file_' + df.index.astype(str) 
print df 
    name age detail 
0 satya 24 file_0 
1 abc 26 file_1 
2 xyz 29 file_2 
3 def 32 file_3 

下一個解決方案是使用map

df['detail'] = 'file_' + df.index.map(str) 

比較:

In [111]: %timeit df['detail']= 'file_' + df.index.astype(str) 
10000 loops, best of 3: 173 µs per loop 

In [112]: %timeit df['detail1'] = 'file_' + df.index.map(str) 
The slowest run took 7.34 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 108 µs per loop 
+0

@ jezrael-u能請說爲什麼這個DF [ 'detail'] = str(s +'_'+ df.index [0] .astype(str))does not worked – Satya

+0

Be導致'df.index [0]'表示索引的第一項 - 它是'0'。 – jezrael

+0

得到了從df ['detail'] = str(s +'_'+ df.index [0] .astype(str))我必須刪除[0]後index.it始終取值在索引[0 ] – Satya