Python Forum
ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements (/thread-37244.html)



ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements - ilknurg - May-17-2022

import mysql.connector
import pandas as pd 


def to_camel_case(col_name: str) -> str:
    #""function to convert column names to camel case"""
    if '_' in col_name:
        components = col_name.split('_')
    elif ' ' in col_name:
        components = col_name.split(' ')
    else:
        return col_name
    # We capitalize the first letter of each component except the first one
    # with the 'title' method and join them together.
    return components[0] + ''.join(x.title() for x in components[1:])

my_conn = mysql.connector.connect(
      host="localhost",
      user="admin",
      passwd="******",
      database="dbname"
    )
cursor = my_conn.cursor(buffered=True)

####### end of connection ####
#my_data = pd.read_sql("SELECT * FROM admin_user",my_conn)
#print(my_data)

tables_mysql = pd.read_sql_query("SHOW TABLES", my_conn)
print(tables_mysql)
for table in tables_mysql["Tables_in_{}".format('dbname')]:
    print(table)
    query = f"SELECT * FROM {table}"
    table_chunks = pd.read_sql_query(query, my_conn, chunksize=100000)
    for chunk in table_chunks:
       table_cols = chunk.columns
       print(table_cols)
       new_col_names = []
       for col_name in table_cols:
            new_col_names.append(to_camel_case(col_name))
            chunk.columns = new_col_names

            print(chunk.columns)
            print('====')
When i run this code i got this error. What is the solution?

   f"Length mismatch: Expected axis has {old_len} elements, new "
ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements



RE: ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements - Larz60+ - May-17-2022

Please always post entire, unmodified error so we can see program flow. Your snippit lacks line number, and looks modified.
Also, why not use one of the CamelCase packages, see: https://pypi.org/search/?q=camelcase