将固定字符串附加到数据帧列,它运行缓慢

时间:2016-03-12 09:51:50

标签: python pandas

这段代码有什么问题?我试图在python的数据帧的每一列之前附加一个固定的字符串,但它运行得太慢

import pandas as pd

DFTrain = pd.read_csv('train.csv')
DFTrain = DFTrain.fillna(DFTrain.mean())

for i in range (2,114):
    append_string = str(i-1)+":"
    DFTrain.iloc[:,i] = append_string + DFTrain.iloc[:,i].astype(str)

1 个答案:

答案 0 :(得分:0)

我认为你可以使用list理解和shape

样品:

print df.columns 
Index([u'cmte_id', u'trans_typ', u'entity_typ', u'state', u'employer',
       u'occupation', u'amount', u'fec_id', u'cand_id'],
      dtype='object')

print df.shape
(8, 9)
numbers = [str(x - 1) + ':' if x > 1 else '' for x in range(0, df.shape[1])]
print numbers
['', '', '1:', '2:', '3:', '4:', '5:', '6:', '7:']

print ['{}{}'.format(x, y) for x, y in zip(numbers, df.columns)]
['cmte_id', 'trans_typ', '1:entity_typ', '2:state', '3:employer', '4:occupation', '5:amount', '6:fec_id', '7:cand_id']

df.columns = ['{}{}'.format(x, y) for x, y in zip(numbers, df.columns)]
print df.columns
Index([u'cmte_id', u'trans_typ', u'1:entity_typ', u'2:state', u'3:employer',
       u'4:occupation', u'5:amount', u'6:fec_id', u'7:cand_id'],
      dtype='object')