8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

将数据类型从 Object 转换为 Int64 (astype、to_numeric 不起作用)

riffnl 2月前

27 0

因此,我尝试在 parseData 方法中打印出我的“place”的 info(),并且我需要将数据类型转换为 int64 而不是对象,而是 astype、to_numeric 和 pd.DataFrame(data=place, dtype=np。

因此,我尝试在 parseData 方法中打印出“place”的 info(),并且我需要将数据类型转换为 int64 而不是对象,但 astype、to_numeric 和 pd.DataFrame(data=place, dtype=np.int64) 都失败了

import numpy as np
import pandas as pd
year = []
month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
date = []
locationList = []
class VisitorsAnalyticsUtils:
    def __init__(self):
        yearPeriod = int(input("Enter year period (1: 1979-1987, 2: 1988-1997, 3: 1998-2007, 4: 2008-2017): "))
        if yearPeriod not in np.array([1,2,3,4]):
            print("End of Program")
            quit()
        else:
            areaRegion = int(input("Enter region (1: Asia, 2: Europe, 3: Others): "))
            if areaRegion not in np.array([1,2,3]):
                print("End of Program")
                quit()
            else:
                if yearPeriod == 1:
                    for x in range(9):
                          year.append(1979+x)
                elif yearPeriod == 2:
                    for x in range(9):
                          year.append(1988+x)
                elif yearPeriod == 3:
                    for x in range(9):
                          year.append(1998+x)    
                else:
                    for x in range(9):
                          year.append(2008+x)
                          
        areaA = np.array([ ' Brunei Darussalam ', ' Indonesia ', ' Malaysia ', ' Philippines ', ' Thailand ', ' Viet Nam ', ' Myanmar ', ' Japan ', ' Hong Kong ', ' China ', ' Taiwan ',' Korea, Republic Of ', ' India ', ' Pakistan ', ' Sri Lanka ', ' Saudi Arabia ', ' Kuwait ', ' UAE ']).tolist()
        areaE = np.array([' United Kingdom ', ' Germany ', ' France ', ' Italy ', ' Netherlands ', ' Greece ', ' Belgium & Luxembourg ', ' Switzerland ', ' Austria ', ' Scandinavia ', ' CIS & Eastern Europe ']).tolist()
        areaO = np.array([ ' USA ', ' Canada ', ' Australia ', ' New Zealand ', ' Africa ']).tolist()
        area = [areaA, areaE, areaO]

        self.yearPeriod = yearPeriod - 1
        self.areaRegion = areaRegion - 1
        self.year = year
        self.area = area
                    
    def loadDataFile(self):
        self.df = pd.read_csv('Int_Monthly_Visitor.csv', index_col=0).replace({'na':0.0}, regex=True)
        print("*** first 5 row of data loaded ***")
        print()
        print(self.df.head())
        
    def parseData(self):
        location = self.area[self.areaRegion]
        place = self.df.filter(items=location)
        place = pd.DataFrame(data=place, dtype=np.int64)
        print(place.info())
          
VAU = VisitorsAnalyticsUtils()
VAU.loadDataFile()
VAU.parseData()

为了更清晰的视野

 def loadDataFile(self):
        self.df = pd.read_csv('Int_Monthly_Visitor.csv', index_col=0).replace({'na':0.0}, regex=True)
        print("*** first 5 row of data loaded ***")
        print()
        print(self.df.head())
        
    def parseData(self):
        location = self.area[self.areaRegion]
        place = self.df.filter(items=location)
        place = pd.DataFrame(data=place, dtype=np.int64)
        print(place.info())

给出的错误是:ValueError: 以 10 为基数的 int() 的文字无效:'4,600'

我对格式和英语的混乱深感抱歉

帖子版权声明 1、本帖标题:将数据类型从 Object 转换为 Int64 (astype、to_numeric 不起作用)
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由riffnl在本站《numpy》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: