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
  How to change the datatype of list elements? mHosseinDS86 9 468 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Getting proper x,y axis values pyhill00 8 374 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 624 May-27-2022, 09:09 AM
Last Post: paul18fr
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 572 May-13-2022, 06:06 PM
Last Post: deanhystad
Question Change elements of array based on position of input data Cola_Reb 6 937 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 2,065 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  Creating permutations of N size with no same elements next to one another (recursion) melmoman 1 818 Dec-18-2021, 09:39 PM
Last Post: Larz60+
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 2,573 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Add elements to a Dictionary muzikman 12 2,132 Sep-10-2021, 03:17 PM
Last Post: muzikman
  Why am I getting list elements < 0 ? Mark17 8 1,851 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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