0

I'm having this error in my script "DtypeWarning: Columns (23) have mixed types. Specify dtype option on import or set low_memory=False", though the file is being created, I feel like it needs to be corrected. I have tried inserting this syntax in my code but it doesn't work:

new_df = pd.read_csv('partial.csv', low_memory=False)

What and where should I add in my code?

import os
import pandas as pd
import tkinter
from tkinter import messagebox

root = tkinter.Tk()
root.withdraw()


directory = 'C:/path'
ext = ('.csv')

for filename in os.listdir(directory):
f = os.path.join(directory, filename)

if f.endswith(ext):

    head_tail = os.path.split(f)
    head_tail1 = 'C:/Output'
    k =head_tail[1]
    r=k.split(".")[0]

    p=head_tail1 + "/" + r + " - .csv"
    mydata = pd.read_csv(f)

    # to pull columns and values
    new = mydata[["A","B","C","D"]]
    new = new.rename(columns={'D': 'F'})
    new['F'] = 1
    print(new.columns)
    new["B"] = (pd.to_datetime(new["B"], format="%d-%b",  errors="coerce").dt.strftime("%#m-%#d").fillna(new["B"]))
    new.to_csv(p ,index=False)

    #to merge columns and values
    merge_columns = ['A', 'B', 'C']
    merged_col = ''.join(merge_columns).replace('ABC', 'G')
    new[merged_col] = new[merge_columns].astype(str).apply(lambda x: '.'.join(x), axis=1)
    new.drop(merge_columns, axis=1, inplace=True)
    new = new.groupby(merged_col).count().reset_index()
    new.to_csv(p, index=False)

    messagebox.showinfo("Parts has been counted", "New csv file can be found in C:/Path")
  • 1
    There is no such line in your code: `new_df = pd.read_csv('partial.csv')`. You can apply [different options](https://stackoverflow.com/questions/24251219/pandas-read-csv-low-memory-and-dtype-options) to the line of code that the warning points to. Most likely it is: `mydata = pd.read_csv(f)`. – 8349697 Dec 05 '22 at 19:08
  • This worked for me! Thanks :) mydata = pd.read_csv(f, engine='python') – QuickSilver42 Dec 06 '22 at 00:18

0 Answers0