Python 将索引和字符串连接到新列

Python 将索引和字符串连接到新列,python,string,pandas,dataframe,Python,String,Pandas,Dataframe,我有一个3列的数据框架(包括索引): 因此,需要添加一个新列detail,该列将存储详细信息文件名,该列中的值应类似(str(文件索引号)) 为了实现这一点,我尝试了以下方法 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+'_'+

我有一个3列的数据框架(包括索引):

因此,需要添加一个新列
detail
,该列将存储详细信息文件名,该列中的值应类似
(str(文件索引号))

为了实现这一点,我尝试了以下方法

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
为循环和iterrows实现

 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
因此,请建议。

您可以与
索引一起使用:

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)

#python 3.6+ solution
df['detail'] = [f"file_{i}" for i in df.index]
比较:

#[40000 rows x 2 columns]
df = pd.concat([df] * 10000, ignore_index=True)

In [153]: %timeit df['detail']= 'file_' + df.index.astype(str)
31.2 ms ± 423 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [154]: %timeit df['detail1'] = 'file_' + df.index.map(str)
16.9 ms ± 411 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [155]: %timeit df['detail'] = [f"file_{i}" for i in df.index]
2.95 ms ± 180 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@jezrael你能告诉我为什么这个df['detail']=str(s+'.+df.index[0].astype(str))不起作用,因为
df.index[0]
是索引的第一项-它是
0
。从df['detail']=str(s+'.+df.index[0].astype(str])我必须删除索引后面的[0]。它总是在索引[0]处取值
df['detail'] = 'file_' + df.index.map(str)

#python 3.6+ solution
df['detail'] = [f"file_{i}" for i in df.index]
#[40000 rows x 2 columns]
df = pd.concat([df] * 10000, ignore_index=True)

In [153]: %timeit df['detail']= 'file_' + df.index.astype(str)
31.2 ms ± 423 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [154]: %timeit df['detail1'] = 'file_' + df.index.map(str)
16.9 ms ± 411 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [155]: %timeit df['detail'] = [f"file_{i}" for i in df.index]
2.95 ms ± 180 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)