Tag Archives: csv

[Solved] Pandas Read CSV Error: TypeError: Empty ‘DataFrame’: no numeric data to plot

Simple code, using the panda module to read the CSV data file, there are two ways, one is abandoned by the new version of panda series. From_ csv; The other is panda. Read_ csv

First of all, the problem is that when reading a CSV file, the default data is object type, so there is no character type data to plot. At this time, you only need to convert the type, as follows:

from pandas import Series
import matplotlib.pyplot as plt
data = Series.from_csv('daily.csv',header=0)
#print(data.head())
data.astype(float)
data.plot()
plt.show()

To solve this problem perfectly, the following figure is drawn:

This is the desired result, so the other is panda. Read_ CSV, you are not so lucky. You will find this kind of data stored in CSV:

1981/1/1,20.7
1981/1/2,17.9
1981/1/3,18.8
1981/1/4,14.6
1981/1/5,15.8
1981/1/6,15.8
1981/1/7,15.8
1981/1/8,17.4
1981/1/9,21.8
1981/1/10,20
1981/1/11,16.2
1981/1/12,13.3
1981/1/13,16.7
1981/1/14,21.5

It can’t be displayed as X-axis label, so I changed the first column to 1981… To solve the problem. At this time, of course, we don’t recommend using the second method. If it’s so troublesome, we can use pandas to draw directly by PLT

Finally, we must pay attention to whether the data is abnormal. Some abnormal data are text exceptions, while some may be semantic exceptions. We need to mine and remove the abnormal data. This is often very important. Of course, drawing is often one of the more intuitive ways

Python read CSV file prompt “line contains null byte” error

Open source software supply chain lighting plan, waiting for you>>>

When Python reads a file, it appears_ When CSV. Error: line contains null byte

# -*- coding:utf-8 -*-

import csv

with open(r'E:\abc\web_test\userinfo.csv','rb') as f:
    reader = csv.reader(f)
    rows = [row for row in reader]
    print rows

Error:

D:\Python27\python.exe E:/abc/loop_reader.py
Traceback (most recent call last):
  File "E:/abc/web_test/loop_reader.py", line 7, in <module>
    rows = [row for row in reader]
_csv.Error: line contains NULL byte

Process finished with exit code 1

The error prompt is:_ csv.Error: line contains NULL byte

CSV error, line contains empty bytes

Reason: it is usually because the extension is XLS or xlsx when saving, and changing it to a CSV file is usually a rename

Solution: save it as a. CSV file

[Solved] MySQL Import csv File[Error Code] 1290 – The MySQL server is running with the –secure-file-priv option

Error: [error code] 1290 – the MySQL server is running with the — secure file priv option

mysql>show variables like '%secure%';;

secure_file_prive=null --Restrict mysqld to disallow imports and exports

secure_file_priv=/tmp/ -- Restrict mysqld import and export to occur only in the /tmp/ directory

secure_file_priv=' ' -- do not restrict mysqld import/export

Solution:

Open my.ini file: add: Secure File priv = “D/JB” to the file

1. Import the test.csv file into mysql

load data infile 'D:/jb/a.csv' -- CSV file storage path
into table test -- the name of the table into which the data is to be imported
fields terminated by ',' optionally enclosed by '"' escaped by '"' -- fields separated by commas, strings enclosed by double quotes, strings themselves enclosed by two double quotes
lines terminated by '\r\n'; -- data lines are separated by \r\n

The successful results are as follows:

2. Import tes.csv file into MySQL (including Chinese)

a. Open the CSV file with a text editor, save it in utf8 format, and then import it

load data infile 'D:/jb/a.csv' -- CSV file storage path
into table test character set 'utf8' -- the name of the table to import the data into, set the encoding
fields terminated by ',' optionally enclosed by '"' escaped by '"' -- fields separated by commas, strings enclosed by double quotes, strings themselves enclosed by two double quotes
lines terminated by '\r\n'; -- data lines are separated by \r\n

3. Export the data in the library to a CSV file (including Chinese)

select * from test 
into outfile 'D:/jb/b.csv' character set 'gbk'
FIELDS TERMINATED BY ','   
OPTIONALLY ENCLOSED BY '"'   
LINES TERMINATED BY '\r\n';

The results are as follows:

Problem solving: error in reading CSV file by Panda: typeerror: invalid type comparison

When reading and processing the data in the CSV file with panda in Python, you may encounter such an error:

TypeError: invalid type comparison

Invalid type comparison

At this time, you can print the data in your dataframe

1. There may be no data in some items, which will be displayed as Nan when printing. Nan can’t be compared with any data, and it’s not equal to any value, including himself (so you can also use a= A to judge whether a is Nan

Therefore, in the following data processing, if a comparison operation is performed, an error will be reported:

Typeerror: invalid type comparison
the solution is to add parameters when reading the CSV

keep_ default_ Na = false
in this way, entries without data will be recognized as empty characters instead of Nan

2. Maybe the data types of different columns in your dataframe are different. Some of them are recognized as STR and some as int. although they all look like numbers, they will also report errors when compared later

At this time, you can add a parameter

Converters = {from ‘: STR,’ to ‘: STR} # convert both from column and to column to STR type

converters are interpreted as follows:

converters : dict, default None
Dict of functions for converting values in certain columns. Keys can either
be integers or column labels

After the same type, they can be compared together