I’m having trouble uploading CSV data to MySQL using pandas. The process works fine for most files, but fails when the CSV starts with blank cells in a specific column.
data = pd.read_csv(file_path, delimiter=',', encoding='utf-8', keep_default_na=False)
# Clean up the 'region' column by removing unwanted text
data['region'] = data['region'].str.rstrip('Co.DistrictDist')
# Create database connection and upload
db_url = 'mysql+mysqlconnector://user:pass@192.168.1.10:3306/testdb'
db_engine = create_engine(db_url, echo=False)
db_conn = db_engine.connect()
data.to_sql(name="locations", con=db_engine, if_exists='append', index=False)
db_conn.close()
The weird thing is that empty values later in the file don’t cause problems. Only when the first row has missing data in the region field does it break. I tried using keep_default_na=False but still get this connection error:
(mysql.connector.errors.OperationalError) 2055: Lost connection to MySQL server at '192.168.1.10:3306', system error: 10053 Connection was terminated by host
Any ideas what might be causing this?