Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

'DataFrame' object has no attribute 'courier_name' - follows a split

$
0
0

I'm attempting to build a series of tables using Python and mariaDB. Everything runs properly up until the point where I try to insert data into the "couriers" table. The couriers table has these fields:

def create_table_couriers():    query = """            CREATE TABLE IF NOT EXISTS couriers (                courier_id VARCHAR(15) NOT NULL PRIMARY KEY,                courier_age INT NOT NULL,                courier_fname VARCHAR(11) NOT NULL,                courier_sname VARCHAR(11) NOT NULL,                courier_phone_number VARCHAR(22) NOT NULL            )"""    return queryHere is the code to insert data into the "couriers" table:query_couriers = """    INSERT INTO couriers    VALUES ("courier_id", "courier_age", "courier_fname", "courier_sname", "courier_phone_number")"""df_couriers[["courier_fname", "courier_sname"]] = df_couriers.courier_name.str.split(" ", expand=True)df_couriers.drop("courier_name", inplace=True, axis=1)cols_to_select = ["courier_id", "courier_age", "courier_fname", "courier_sname", "courier_phone_number"]insert_data(df_couriers, query_couriers)

This returns the following error:

AttributeError                            Traceback (most recent call last)~\AppData\Local\Temp\ipykernel_10984\3596799012.py in ?()      5 """      6       7 # for the couriers dataframe, make sure to split the courier name into first name and surname using the split function      8 # to be completed----> 9 df_couriers[["courier_fname", "courier_sname"]] = df_couriers.courier_name.str.split(" ", expand=True)     10      11 # drop the columns that are of no use     12 # to be completedc:\Users\Ryzen5\anaconda3\envs\isi\lib\site-packages\pandas\core\generic.py in ?(self, name)   6200             and name not in self._accessors   6201             and self._info_axis._can_hold_identifiers_and_holds_name(name)   6202         ):   6203             return self[name]-> 6204         return object.__getattribute__(self, name)AttributeError: 'DataFrame' object has no attribute 'courier_name'

What confuses me is that a previous table with the same basic structure worked fine when I tried to insert data. Here's the "customers" table:

def create_table_customers():    query = """            CREATE TABLE IF NOT EXISTS customers (                customer_id INT NOT NULL AUTO_INCREMENT,                customer_fname VARCHAR(20) NOT NULL,                customer_sname VARCHAR(20) NOT NULL,                customer_street_number MEDIUMINT UNSIGNED NOT NULL,                customer_street_name VARCHAR(30) NOT NULL,                customer_postal_code MEDIUMINT UNSIGNED NOT NULL,                customer_phone_number VARCHAR(30) NOT NULL,                PRIMARY KEY(customer_id)            )"""    return queryAnd the attempt to insert data into it that ran successfully:query_customers = """    INSERT INTO customers     VALUES (DEFAULT, ?, ?, ?, ?, ?, ?)"""df_customers[["customer_fname", "customer_sname"]] = df_customers.customer_name.str.split(" ", expand=True)df_customers.drop("customer_name", inplace=True, axis=1)cols_to_select = ["customer_fname", "customer_sname", "customer_street_number", "customer_street_name", "customer_postal_code", "customer_phone_number"]df_customers = df_customers.reindex(columns=cols_to_select)insert_data(df_customers, query_customers)

Both the "customers" and "couriers" table involved a column that was split ("customer_name" and "courier_name" became "customer/courier_fname" as in first name and "customer/courier_sname" as in last name).

In both cases a print of the columns in that DataFrame returned ONLY the fname and sname attributes.

Except for some reason, ONLY the "couriers" table returned an AttributeError when I ran it. Why is this?


Viewing all articles
Browse latest Browse all 23160

Trending Articles