繁体   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