pandas -放置功能错误(标签未包含在轴中) [英] Pandas - Drop function error (label not contained in axis)

查看:55
本文介绍了 pandas -放置功能错误(标签未包含在轴中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的CSV文件:

I have a CSV file that is as the following:

index,Avg,Min,Max
Build1,56.19,39.123,60.1039
Build2,57.11,40.102,60.2
Build3,55.1134,35.129404123,60.20121

基于我的问题此处我可以通过以下简短脚本向此csv添加一些相关信息:

Based off my question here I am able to add some relevant information to this csv via this short script:

import pandas as pd

df = pd.read_csv('newdata.csv')
print(df)

df_out = pd.concat([df.set_index('index'),df.set_index('index').agg(['max','min','mean'])]).rename(index={'max':'Max','min':'Min','mean':'Average'}).reset_index()

with open('newdata.csv', 'w') as f:
    df_out.to_csv(f,index=False)

这将产生以下CSV文件:

This results in this CSV:

index,Avg,Min,Max
Build1,56.19,39.123,60.1039
Build2,57.11,40.102,60.2
Build3,55.1134,35.129404123,60.20121
Max,57.11,40.102,60.20121
Min,55.1134,35.129404123,60.1039
Average,56.1378,38.1181347077,60.16837

我现在想拥有它,以便可以更新此csv.例如,如果我运行一个新的版本(例如build4),则可以添加它,然后重做最大",最小",平均"行.我的想法是因此删除带有标签最大",最小",平均"的行,添加新行,然后重做统计信息.我相信我需要的代码非常简单(仅适用于Max,但也将包含Min和Average行):

I would like to now have it so I can update this csv. For example if I ran a new build (build4 for instance) I could add that in and then redo the Max, Min, Average rows. My idea is that I therefore delete the rows with labels Max, Min, Average, add my new row, redo the stats. I believe the code I need is as simple as (just for Max but would have lines for Min and Average as well):

df = pd.read_csv('newdata.csv')
df = df.drop('Max')

但是,这总是会导致 ValueError:轴中不包含标签['Max']

我已经以崇高的文字创建了csv文件,这可能是问题的一部分吗?我已经阅读了其他有关此的SO帖子,但似乎没有一个对我的问题有所帮助.

I have created the csv files in sublime text, could this be part of the issue? I have read other SO posts about this and none seem to help my issue.

我不确定是否允许,但这是一个下载指向我的链接csv ,以防文件本身出现问题.

I am unsure if this allowed but here is a download link to my csv just in case something is wrong with the file itself.

我可以给出两个可能的答案:

I would be okay with two possible answers:

  1. 如何解决此掉包问题
  2. 如何添加更多内部版本和更新统计信息(一种没有删除的方法)

推荐答案

您必须指定axis参数.默认值是axis = 0,这是行列,是axis = 1.

You must specify the axis argument. default is axis = 0 which is rows columns is axis = 1.

所以这应该是您的代码.

so this should be your code.

df = df.drop('Max',axis=1)

看这段代码:

df = pd.read_csv('newdata.csv')
df = df.drop('Max')

您使用的代码未指定csv文件的第一列包含该数据帧的索引.因此,大熊猫可以即时创建索引.该索引纯粹是数字索引.因此,您的索引不包含最大值".

The code you used does not specify that the first column of the csv file contains the index for the dataframe. Thus pandas creates an index on the fly. This index is purely a numerical one. So your index does not contain "Max".

尝试以下操作:

df = pd.read_csv("newdata.csv",index_col=0)
df = df.drop("Max",axis=0)

这将强制熊猫使用csv文件中的第一列作为索引.这应该意味着代码现在可以正常工作了.

This forces pandas to use the first column in the csv file to be used as index. This should mean the code works now.

这篇关于 pandas -放置功能错误(标签未包含在轴中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆