Remove Line Break After CSV File Aggregation
I am aggregating data in a
CVS file, the code:
import pandas df = pandas.read_csv("./input.csv", delimiter=";", low_memory=False) df.head() count_severity = df.groupby("PHONE")["IMEI"].unique() has_multiple_elements = count_severity.apply(lambda x: len(x)>1) result = count_severity[has_multiple_elements] result.to_csv("./output.csv", sep=";")
and in some lines of the received file, I get the following:
It turns out that I get the second column, which is after the sign
;, divided into
Could you tell me please, how to get rid of this line break? I tried adding a parameter
result.to_csv - it didn't help.
Any method is accepted, even if you have to overwrite this file and save a new one. I also tried this:
import pandas as pd output_file = open("./output.csv", "r") output_file = ''.join([i for i in output_file]).replace("\n", "") output_file_new = open("./output_new.csv", "w") output_file_new.writelines(output_file) output_file_new.close()
But then I get solid lines, which is not good at all.
To summarize, I should get the format of this file:
If your wrong lines always start with a comma, you could just replace the sequence
with open("./output.csv", "r") as file: content = file.read() new_content = content.replace("\n,", ",") with open("./new_output.csv", "w") as file: file.write(new_content)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module