我需要将数据帧写入csv文件,因为我已完成以下操作:
..............................................
temp_df = pd.DataFrame(variance.values,df.columns.values)
temp_df.to_csv('var.csv')
...................................
这个工作正常,但我仍然需要一个小东西,明智地编写csv文件,将columns参数添加到to_csv doens真的有帮助:
temp_df.to_csv('var'+tempname+'.csv',columns=df.columns.values )
提供以下内容:
KeyError: "None of [['Feature0', 'Feature1', 'Feature2', 'Feature3', 'Feature4', 'Feature5', 'Feature6', 'Feature7', 'Feature8', 'Feature9', 'Feature10', 'Feature11', 'Feature12', 'Feature13', 'Feature14', 'Feature15', 'Feature16', 'Feature17', 'Feature18', 'Feature19', 'Feature20', 'Feature21', 'Feature22', 'Feature23', 'Feature24', 'Feature25', 'Feature26', 'Feature27', 'Feature28', 'Feature29', 'Feature30', 'Feature31', 'Feature32', 'Feature33', 'Feature34', 'Feature35', 'Feature36', 'Feature37', 'Feature38', 'Feature39', 'Feature40', 'Feature41', 'Feature42', 'Feature43', 'Feature44', 'Feature45', 'Feature46', 'Feature47', 'Feature48', 'Feature49', 'Feature50', 'Feature51', 'Feature52', 'Feature53', 'Feature54', 'Feature55', 'Feature56', 'Feature57', 'Feature58', 'Feature59', 'Feature60', 'Feature61', 'Feature62', 'Feature63', 'Feature64', 'Feature65', 'Feature66', 'Feature67', 'Feature68', 'Feature69', 'Feature70', 'Feature71', 'Feature72', 'Feature73', 'Feature74', 'Feature75', 'Feature76', 'Feature77', 'Feature78', 'Feature79', 'Feature80', 'Feature81', 'Feature82', 'Feature83', 'Feature84', 'Feature85', 'Feature86', 'Feature87', 'Feature88', 'Feature89', 'Feature90', 'Feature91', 'Feature92', 'Feature93', 'Feature94', 'Feature95', 'Feature96', 'Feature97', 'Feature98', 'Feature99', 'Feature100', 'Feature101', 'Feature102', 'Feature103', 'Feature104', 'Feature105', 'Feature106', 'Feature107', 'Feature108', 'Feature109', 'Feature110', 'Feature111', 'Feature112', 'Feature113', 'Feature114', 'Feature115', 'Feature116', 'Feature117', 'Feature118', 'Feature119', 'Feature120', 'Feature121', 'Feature122', 'Feature123', 'Feature124', 'Feature125', 'Feature126', 'Feature127', 'Feature128', 'Feature129', 'Feature130', 'Feature131', 'Feature132', 'Feature133', 'Feature134', 'Feature135', 'Feature136', 'Feature137', 'Feature138', 'Feature139', 'Feature140', 'Feature141', 'Feature142', 'Feature143', 'Feature144', 'Feature145', 'Feature146', 'Feature147', 'Feature148', 'Feature149', 'Feature150', 'Feature151', 'Feature152', 'Feature153', 'Feature154', 'Feature155', 'Feature156', 'Feature157', 'Feature158', 'Feature159', 'Feature160', 'Feature161', 'Feature162', 'Feature163', 'Feature164', 'Feature165', 'Feature166', 'Feature167', 'Feature168', 'Feature169', 'Feature170', 'Feature171', 'Feature172', 'Feature173', 'Feature174', 'Feature175', 'Feature176', 'Feature177', 'Feature178', 'Feature179', 'Feature180', 'Feature181', 'Feature182', 'Feature183', 'Feature184', 'Feature185', 'Feature186', 'Feature187', 'Feature188', 'Feature189', 'Feature190', 'Feature191', 'Feature192', 'Feature193', 'Feature194', 'Feature195', 'Feature196', 'Feature197', 'Feature198', 'Feature199', 'Feature200', 'Feature201', 'Feature202', 'Feature203', 'Feature204', 'Feature205', 'Feature206', 'Feature207', 'Feature208', 'Feature209', 'Feature210', 'Feature211', 'Feature212', 'Feature213', 'Feature214', 'Feature215', 'Feature216', 'Feature217', 'Feature218', 'Feature219', 'Feature220', 'Feature221', 'Feature222', 'Feature223', 'Feature224', 'Feature225', 'Feature226', 'Feature227', 'Feature228', 'Feature229', 'Feature230', 'Feature231', 'Feature232', 'Feature233', 'Feature234', 'Feature235', 'Feature236', 'Feature237', 'Feature238', 'Feature239', 'Feature240', 'Feature241', 'Feature242', 'Feature243', 'Feature244', 'Feature245', 'Feature246', 'Feature247', 'Feature248', 'Feature249', 'Feature250', 'Feature251', 'Feature252', 'Feature253', 'Feature254', 'Feature255', 'Feature256', 'Feature257', 'Feature258', 'Feature259', 'Feature260', 'Feature261', 'Feature262', 'Feature263', 'Feature264', 'Feature265', 'Feature266', 'Feature267', 'Feature268', 'Feature269', 'Feature270', 'Feature271', 'Feature272', 'Feature273', 'Feature274', 'Feature275', 'Feature276', 'Feature277', 'Feature278', 'Feature279', 'Feature280', 'Feature281', 'Feature282', 'Feature283', 'Feature284', 'Feature285', 'Feature286', 'Feature287', 'Feature288', 'Feature289', 'Feature290', 'Feature291', 'Feature292', 'Feature293', 'Feature294', 'Feature295', 'Feature296', 'Feature297', 'Feature298', 'Feature299', 'Feature300', 'Feature301', 'Feature302', 'Feature303', 'Feature304', 'Feature305', 'Feature306', 'Feature307', 'Feature308', 'Feature309', 'Feature310', 'Feature311', 'Feature312', 'Feature313', 'Feature314', 'Feature315', 'Feature316', 'Feature317', 'Feature318', 'Feature319', 'Feature320', 'Feature321', 'Feature322', 'Feature323', 'Feature324', 'Feature325', 'Feature326', 'Feature327', 'Feature328', 'Feature329', 'Feature330', 'Feature331', 'Feature332', 'Feature333', 'Feature334', 'Feature335', 'Feature336', 'Feature337', 'Feature338', 'Feature339', 'Feature340', 'Feature341', 'Feature342', 'Feature343', 'Feature344', 'Feature345', 'Feature346', 'Feature347', 'Feature348', 'Feature349', 'Feature350', 'Feature351', 'Feature352', 'Feature353', 'Feature354', 'Feature355', 'Feature356', 'Feature357', 'Feature358', 'Feature359', 'Feature360', 'Feature361', 'Feature362', 'Feature363', 'Feature364', 'Feature365', 'Feature366', 'Feature367', 'Feature368', 'Feature369', 'Feature370', 'Feature371', 'Feature372', 'Feature373', 'Feature374', 'Feature375', 'Feature376', 'Feature377', 'Feature378', 'Feature379', 'Feature380', 'Feature381', 'Feature382', 'Feature383', 'Feature384', 'Feature385', 'Feature386', 'Feature387', 'Feature388', 'Feature389', 'Feature390', 'Feature391', 'Feature392', 'Feature393', 'Feature394', 'Feature395', 'Feature396', 'Feature397', 'Feature398', 'Feature399', 'Feature400', 'Feature401', 'Feature402', 'Feature403', 'Feature404', 'Feature405', 'Feature406', 'Feature407', 'Feature408', 'Feature409', 'Feature410', 'Feature411', 'Feature412', 'Feature413', 'Feature414', 'Feature415', 'Feature416', 'Feature417', 'Feature418', 'Feature419', 'Feature420', 'Feature421', 'Feature422', 'Feature423', 'Feature424', 'Feature425', 'Feature426', 'Feature427', 'Feature428', 'Feature429', 'Feature430', 'Feature431', 'Feature432', 'Feature435', 'Feature436', 'Feature437', 'Feature438', 'Feature439', 'Feature440', 'Feature441', 'Feature442', 'Feature443', 'Feature444', 'Feature445', 'Feature446', 'Feature447', 'Feature448', 'Feature449', 'Feature450', 'Feature451', 'Feature452', 'Feature453', 'Feature454', 'Feature455', 'Feature456', 'Feature457', 'Feature458']] are in the [columns]"
** UDPATE **
结果现在看起来像:
1.Column 2.column
Feature0 26657.97061
Feature1 40253.50694
Feature2 3221147446
Feature3 0.027772714
Feature4 5.959388786
Feature5 266.56
Feature6 734.2481633
Feature7 307.363629
Feature8 0.000566779
Feature9 0.000520574
...........
我想要的是:
1.row Feature0 Feature1 Feature2 Feature3 Feature5 ...........
2.row 26657.97061 40253.50694 3221147446 0.027772714 5.959388786 ......
答案 0 :(得分:2)
In [129]: df
Out[129]:
col1 col2
0 Feature0 2.665797e+04
1 Feature1 4.025351e+04
2 Feature2 3.221147e+09
3 Feature3 2.777271e-02
4 Feature4 5.959389e+00
5 Feature5 2.665600e+02
6 Feature6 7.342482e+02
7 Feature7 3.073636e+02
8 Feature8 5.667790e-04
9 Feature9 5.205740e-04
In [130]: df.T
Out[130]:
0 1 2 3 4 5 6 7 8 9
col1 Feature0 Feature1 Feature2 Feature3 Feature4 Feature5 Feature6 Feature7 Feature8 Feature9
col2 26658 40253.5 3.22115e+09 0.0277727 5.95939 266.56 734.248 307.364 0.000566779 0.000520574
In [131]: df.T.to_csv('d:/temp/out.csv', header=None)
结果(d:/temp/out.csv
):
col1,Feature0,Feature1,Feature2,Feature3,Feature4,Feature5,Feature6,Feature7,Feature8,Feature9
col2,26657.97061,40253.50694,3221147446.0,0.027772714,5.959388786,266.56,734.2481633,307.363629,0.000566779,0.000520574