简体   繁体   中英

cannot convert float NaN to integer

I am trying to upload CSV data in MySQL table but my CSV has some empty columns and when I'm uploading my CSV all data before the empty columns are uploading but after that CSV upload getting a stop

for i in range(m * chunksize, (m * chunksize) + chunksize):
    company = pd.isnull(df.loc[i]['company'] or df.loc[i]['name'] or df.loc[i]['observed_sales'] or df.loc[i]['observed_transactions'])
    if company == True : 
          df.loc[i]['company'] = ''
          df.loc[i]['name'] = ''
          y = np.nan_to_num(df.loc[i]['observed_sales'])
          z = np.nan_to_num(df.loc[i]['observed_transactions'])
          df.loc[i]['observed_sales'] = df.loc[i]['observed_sales'].replace('nan', np.nan).interpolate(0.0)
          df.loc[i]['observed_transactions'] = df.loc[i]['observed_transactions'].replace('nan', np.nan).interpolate(0.0)
          Company.objects.update_or_create(company_name=df.loc[i]['company'], company_full_name=df.loc[i]['name'], website_url=df.loc[i]['website'])
          obj = Company.objects.latest('company_id')
          id = obj.company_id
          TransactionDetails_Monthly.objects.update_or_create(company_id=id, observed_sales=y, observed_transactions=z, observed_customers=df.loc[i]['observed_customers'], sales_per_customer=df.loc[i]['sales_per_customer'], txns_per_customer=df.loc[i]['txns_per_customer'], avg_txn_value=df.loc[i]['avg_txn_value'], month=df.loc[i]['month'])
          msg = "Data is inserted successfully"

I'm facing this error [cannot convert float NaN to integer]

and I also want to show my models.py

class Company(models.Model):
    company_id =  models.AutoField(primary_key=True)
    category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True) #ForeignKey
    company_name = models.CharField(max_length=255,null=True)
    company_full_name = models.CharField(max_length=255, null=True)
    company_name_url = models.CharField(max_length=255, null=True)
    website_url = models.CharField(max_length=255, null=True)
    founded_date =  models.DateField(null=True)       
    founded_date_precision =  models.DateField(null=True)       
    total_funding_amount = models.DecimalField(max_digits=13, decimal_places=2,  null=True)
    total_funding_amount_currency = models.DecimalField(max_digits=13, decimal_places=2,  null=True) 
    total_funding_amount_currency_usd = models.DecimalField(max_digits=13, decimal_places=2,  null=True) 

class TransactionDetails_Monthly(models.Model):
    transaction_id = models.AutoField(primary_key=True)
    company = models.ForeignKey('Company' , on_delete = models.CASCADE, null=True)   #ForeignKey
    month = models.DateField()
    observed_sales = models.IntegerField()
    observed_transactions = models.IntegerField()
    observed_customers = models.IntegerField()
    sales_per_customer = models.FloatField(null=True) 
    txns_per_customer = models.FloatField(null=True, blank=True, default=None)
    avg_txn_value = models.DecimalField(max_digits=13, decimal_places=2, blank=True, null=True)

The issue is that there are some empty/Nan values in CSV.

You have to add some check for each value like:

for

z = np.nan_to_num(df.loc[i]['observed_transactions'])

you can do:

if 'observed_transactions' in np.nan_to_num(df.loc[i]:
    if not np.nan_to_num(df.loc[i]['observed_transactions']) == 'NaN':
        z = np.nan_to_num(df.loc[i]['observed_transactions'])
    else:
        z = None
else:
    z = None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM