Python Forum
ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 428 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,139 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Print names in x-axis of a time-series values hobbyist 4 1,218 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  ValueError - Formatting issue when values enter thousands phillyfa 4 1,169 Apr-20-2023, 06:22 PM
Last Post: phillyfa
  Checking if a string contains all or any elements of a list k1llcod3 1 1,094 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Summing up set elements(HH:MM:SS) tester_V 4 1,166 Dec-22-2022, 10:03 AM
Last Post: perfringo
  How to change the datatype of list elements? mHosseinDS86 9 1,955 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Getting proper x,y axis values pyhill00 8 1,635 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  How can I add certain elements in this 2d data structure and calculate a mean TheOddCircle 3 1,543 May-27-2022, 09:09 AM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020