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
所以請建議。
@ jezrael-u能請說爲什麼這個DF [ 'detail'] = str(s +'_'+ df.index [0] .astype(str))does not worked – Satya
Be導致'df.index [0]'表示索引的第一項 - 它是'0'。 – jezrael
得到了從df ['detail'] = str(s +'_'+ df.index [0] .astype(str))我必須刪除[0]後index.it始終取值在索引[0 ] – Satya