我有以下问题.
可以说这是我的CSV
id f1 f2 f3
1 4 5 5
1 3 1 0
1 7 4 4
1 4 3 1
1 1 4 6
2 2 6 0
..........
因此,我有可以按ID分组的行.
我想创建如下的csv作为输出.
f1 f2 f3 f1_n f2_n f3_n f1_n_n f2_n_n f3_n_n f1_t f2_t f3_t
4 5 5 3 1 0 7 4 4 1 4 6
因此,我希望能够选择要转换为列的行数(始终从id的第一行开始).在这种情况下,我抓了3行.
然后,我还将跳过一个或多个行(在这种情况下,仅跳过一个),以从同一id组的最后一行获取最后一列.由于某些原因,我想使用一个数据框.
经过3-4个小时的奋斗.我找到了下面给出的解决方案.
但是我的解决方案很慢.我大约有700,000行,可能有大约70,000组ID.在我的4GB 4核心Lenovo上,model = 3上的上述代码将花费近一个小时.我需要进入模型=可能是10或15.我仍然是Python的新手,并且我相信可以进行一些更改来加快速度.有人可以深入解释我如何改进代码.
万分感谢.
型号:要抓取的行数
# train data frame from reading the csv
train = pd.read_csv(filename)
# Get groups of rows with same id
csv_by_id = train.groupby('id')
modelTarget = { 'f1_t','f2_t','f3_t'}
# modelFeatures is a list of features I am interested in the csv.
# The csv actually has hundreds
modelFeatures = { 'f1, 'f2' , 'f3' }
coreFeatures = list(modelFeatures) # cloning
selectedFeatures = list(modelFeatures) # cloning
newFeatures = list(selectedFeatures) # cloning
finalFeatures = list(selectedFeatures) # cloning
#