Python “系列”对象是可变的,因此它们不能被散列错误调用 to_csv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32568012/
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
'Series' objects are mutable, thus they cannot be hashed error calling to_csv
提问by Robert
I have a large Dataframe (5 days with one value per second, several columns) of which I'd like to save 2 columns in a csv file with python pandas df.to_csv module.
我有一个大数据框(5 天,每秒一个值,几列),我想用 python pandas df.to_csv 模块在 csv 文件中保存 2 列。
I tried different ways but always get the error message:
我尝试了不同的方法,但总是收到错误消息:
'Series' objects are mutable, thus they cannot be hashed
“系列”对象是可变的,因此它们不能被散列
which I found a solution for in connection with groupby but not with filesaving. Somebody has an idea for me?
我找到了一个与 groupby 有关但与文件保存无关的解决方案。有人对我有想法吗?
Here a part of my Dataframe:
这是我的数据框的一部分:
DateTime
2015-07-14 00:00:00 414.37
2015-07-14 00:00:00 414.37
2015-07-14 00:00:01 414.29
2015-07-14 00:00:02 414.14
2015-07-14 00:00:03 414.21
2015-07-14 00:00:04 414.05
2015-07-14 00:00:05 414.05
2015-07-14 00:00:06 414.2
2015-07-14 00:00:07 414.54
2015-07-14 00:00:08 414.39
Name: CO2abs, dtype: object DateTime
Edit: sorry - forgot the code...
编辑:对不起 - 忘记了代码...
df.to_csv('alldatcorr.csv',sep='\t',cols=(df.CO2abs,df.CO2corr))
采纳答案by EdChum
Your error comes about because you passed a tuple of Series rather than a tuple of column names/strings:
你的错误是因为你传递了一个 Series 元组而不是一个列名/字符串元组:
df.to_csv('alldatcorr.csv',sep='\t',cols=(df.CO2abs,df.CO2corr))
So you found that this worked:
所以你发现这有效:
df.to_csv('corr2.csv',sep='\t',cols=('CO2abs','CO2corr'))
you could've avoided the ambiguity by just sub-selecting from your df by passing a list and using the sub-script operator:
您可以通过传递列表并使用子脚本运算符从 df 中进行子选择来避免歧义:
df[['CO2abs','CO2corr']].to_csv('corr2.csv',sep='\t')
Also it's probably more readable to pass a list of strings rather than a tuple
此外,传递字符串列表而不是元组可能更具可读性