简体   繁体   中英

raise KeyError(key) with Pandas

DataFrame looks okay but I'm getting key error when I attempt sorting by the TXBS column in descending order:

在此处输入图片说明

pd.set_option('max_colwidth', -1)

data = pd.read_csv('intC.txt')
print(data)

mysort = data.sort_values(by=['TXBS'], ascending=False)

This is what the intC.txt file looks like:

在此处输入图片说明

You can try this also to avoid KeyError , hope the below code will solve your issue:

import pandas as pd
data = pd.DataFrame([i.split() for i in open("intC.txt", "r").readlines()])
data.columns = data.iloc[0]
data = data.iloc[1: , :]
data['TXBS'] = pd.to_numeric(data['TXBS'], errors='coerce')
mysort = data.sort_values(by=['TXBS'], ascending=False)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM