运行时警告熊猫

问题描述 投票:4回答:1

我继续得到一个RuntimeWarning:unorderable类型:str()<int(),对于无法比较的对象,未定义排序顺序result = result.union(other)来自以下代码。不完全确定我在哪里做错了什么。我正试图让数据集看起来像this.

df = pd.read_excel('/Users/user/Desktop/------/data.xlsx')
df.rename(columns={'Product Installed Fiscal Quarter': 'Quarter', 'Serviceable Product #': 'Product Code', 'Sold To Customer Name': 'Account', 'Orderable Product Description': 'Product'}, inplace=True)
df.drop(['# of Licenses'], axis=1, inplace=True)

def all_products():
    products = []
    for index, row in df.iterrows():
        if row['Serviceable Product Description'] not in products:
            products.append(row['Serviceable Product Description'])
    products.insert(0, 'Account')
    products.insert(1, 'Time')
    return products

header = all_products()
xd = pd.DataFrame(columns= header)

def reformatted_data():    
    for index, row in df.iterrows():
        add = [0] * len(header)
        add.insert(0, row['Account'])
        add.insert(1, row['Quarter'])
        index = header.index(row['Serviceable Product Description'])
        add[index] = row['Quantity']
        xd.append(add)
    return xd
reformatted_data()
python pandas
1个回答
0
投票

从Excel阅读时,重要的是要记住您的数据类型。熊猫在大多数时候都会做出很好的猜测,但建议明确地投射你将要广泛使用的列。例如,excel中的列可以没有关注字符串和数字但是Pandas要求列是统一类型。因此,Pandas会将该列强制转换为字符串。谨防。

© www.soinside.com 2019 - 2024. All rights reserved.