无法将float NaN转换为整数

时间:2019-09-19 07:35:16

标签: python django

我正在尝试在MySQL表中上传CSV数据,但是我的CSV包含一些空列,并且当我上传CSV时,所有数据都在上传空列之前但在该CSV上传之后停止

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"

我遇到此错误[无法将float 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)

1 个答案:

答案 0 :(得分:0)

问题在于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