I import the server's response to csv, and then open it as a DataFrame . The problem is that zero values ​​are written to the file as '--' , which is why I get an error of data compliance.

As I understand it, it will be easier to somehow open the CSV and change these characters to 0.

Tell me, please, how to be :) and maybe somehow implement it with the help of DataFrame

 file = open("cashe.txt", "w") file.write(data) file.close() f = DataFrame.from_csv("cashe.txt",header=1, sep=' ', index_col=0, parse_dates=True) 
  • Questions asking for help with debugging ( “why does this code not work?” ) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create a minimal, self-contained and reproducible example. - MaxU pm

1 answer 1

Reproducible example of CSV data:

 ABC 1 -- 3 4 5 6 7 -- 9 

If you try to read such data, column B will contain strings instead of numbers:

 In [3]: import pandas as pd In [4]: df = pd.read_csv(r'C:\Temp\1.tsv', delim_whitespace=True) In [5]: df Out[5]: ABC 0 1 -- 3 1 4 5 6 2 7 -- 9 In [6]: df.dtypes Out[6]: A int64 B object # <--- NOTE! C int64 dtype: object 

use the na_values=['--'] parameter to tell pd.read_csv() that you have NaN represented as -- :

 In [7]: df = pd.read_csv(r'C:\Temp\1.tsv', delim_whitespace=True, na_values=['--']) In [8]: df Out[8]: ABC 0 1 NaN 3 1 4 5.0 6 2 7 NaN 9 In [9]: df.dtypes Out[9]: A int64 B float64 # <--- NOTE! C int64 dtype: object 

Now you can easily fill NaN zero:

 In [15]: df['B'] = df['B'].fillna(0) In [16]: df Out[16]: ABC 0 1 0.0 3 1 4 5.0 6 2 7 0.0 9 

Digression: it describes in detail how to most effectively ask a question related to processing and / or analyzing data (for example: by Pandas / Numpy / SciPy / SciKit Learn / SQL)?