Series objects are mutable, thus they cannot be hashed error calling to_csv

2024/10/17 18:16:57

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.

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?

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))
Answer

Your error comes about because you passed a tuple of Series rather than a tuple of column names/strings:

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[['CO2abs','CO2corr']].to_csv('corr2.csv',sep='\t')

Also it's probably more readable to pass a list of strings rather than a tuple

https://en.xdnf.cn/q/72630.html

Related Q&A

Python client / server question

Im working on a bit of a project in python. I have a client and a server. The server listens for connections and once a connection is received it waits for input from the client. The idea is that the c…

Segmentation fault during import cv on Mac OS

Trying to compile opencv on my Mac from source. I have following CMakeCache.txt: http://pastebin.com/KqPHjBx0I make ccmake .., press c, then g. Than I make sudo make -j8: http://pastebin.com/cJyr1cEdTh…

Python bug - or my stupidity - EOL while scanning string literal

I cannot see a significant difference between the two following lines. Yet the first parses, and the latter, does not.In [5]: n=""" \\"Axis of Awesome\\" """In […

IOPub Error on Google Colaboratory in Jupyter Notebook

I understand that the below command jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 would let me set the data rate. But on Colab, I cannot run this command since the notebook is already ope…

Python code calls C library that create OS threads, which eventually call Python callbacks

If the one and only Python interpreter is in the middle of executing a bytecode when the OS dispatches another thread, which calls a Python callback - what happens? Am I right to be concerned about th…

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

Ive got a couple django models that look like this:from django.contrib.sites.models import Siteclass Photo(models.Model):title = models.CharField(max_length=100)site = models.ForeignKey(Site)file = mod…

Logging django.request to file instead of console

I am trying to configure my django settings.py to properly use the python logging facility but Ive stumbled upon a rather strange problem:Even after reading the docs, I simply cant find out how to redi…

Coinbase APIerror(id = ) in python

I want to transfer money between my coinbase accounts. Im storing all of my accounts IDs from client.get_accounts()[data][id] and transferring with the code, tx = client.transfer_money(2bbf394c-193b-5b…

cxfreeze missing distutils module inside virtualenv

When running a cxfreeze binary from a python3.2 project I am getting the following runtime error:/project/dist/project/distutils/__init__.py:13: UserWarning: The virtualenv distutils package at %s appe…

Preventing a multiplication expression evaluating in Sympy

I am generating an expression with two fractions, and want to pretty print as a whole expression with LaTeX, to then put on a worksheet.E.g. in the form:(5/7) * (3/4). However, when I do the following:…