从 DataFrame python pandas 写入 csv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21206395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write to csv from DataFrame python pandas
提问by Developer
I wrote a program where i add two columns and write the answer to CSV file but I am getting error when I want to write only selection of columns . here is my logic:
我编写了一个程序,在其中添加两列并将答案写入 CSV 文件,但是当我只想编写列选择时出现错误。这是我的逻辑:
import pandas as pd
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'bar'],
'B' : ['one', 'one', 'two', 'two',
'two', 'two', 'one', 'two'],
'C' : [56, 2, 3, 4, 5, 6, 0, 2],
'D' : [51, 2, 3, 4, 5, 6, 0, 2]})
grouped = df.groupby(['A', 'B']).sum()
grouped['sum'] = (grouped['C'] / grouped['D'])
# print (grouped[['sum']])
a = pd.DataFrame(grouped)
a.to_csv("C:\Users\test\Desktop\test.csv", index=False, cols=('A','B','sum'))
how can i only write data of column A, B and Sum. I get the following error
我怎么能只写A列、B列和总和的数据。我收到以下错误
Traceback (most recent call last):
File "C:\Users\test\Desktop\eclipse\yuy\group.py", line 19, in <module>
a.to_csv("C:\Users\test\Desktop\test.csv", index=False, cols=('A','B','sum'))
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1126, in to_csv
date_format=date_format)
File "C:\Python27\lib\site-packages\pandas\core\format.py", line 992, in __init__
self.obj = self.obj.loc[:, cols]
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1018, in __getitem__
return self._getitem_tuple(key)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 595, in _getitem_tuple
self._has_valid_tuple(tup)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 106, in _has_valid_tuple
if not self._has_valid_type(k, i):
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1100, in _has_valid_type
(key, self.obj._get_axis_name(axis)))
KeyError: "[['A', 'B', 'sum']] are not in ALL in the [columns]"
采纳答案by Matt Swain
A and B are no longer columns, since you called groupby(['A', 'B']). Instead they are both an index. Try leaving out the index=False, like this:
A 和 B 不再是列,因为您调用了groupby(['A', 'B']). 相反,它们都是一个索引。尝试省略index=False,如下所示:
a.to_csv("test.csv", cols=['sum'])
回答by Wesam Na
If you want to write it as an excel file, use this command
如果你想把它写成一个excel文件,使用这个命令
writer = pd.ExcelWriter('output.xlsx')
data_frame.to_excel(writer,'Sheet1')
writer.save()

