簡體   English   中英

無法將浮點 NaN 轉換為 integer

[英]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"

我正面臨這個錯誤[無法將浮點 NaN 轉換為整數]

我也想展示我的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)

問題是 CSV 中有一些空/Nan 值。

您必須為每個值添加一些檢查,例如:

為了

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

你可以做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM