ValueError:无法将isin与pandas一起从重复的轴重新索引 [英] ValueError: cannot reindex from a duplicate axis using isin with pandas

查看:288
本文介绍了ValueError:无法将isin与pandas一起从重复的轴重新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将邮政编码缩短到各种文件中,但是我一直在获取

I am trying to short zipcodes into various files but I keep getting

ValueError:无法从重复的轴重新索引

ValueError: cannot reindex from a duplicate axis

我已经阅读了有关Stackoverflow的其他文档,但是我还没有弄清楚为什么它的重复轴.

I've read through other documentation on Stackoverflow, but I haven't been about to figure out why its duplicating axis.

import csv
import pandas as pd
from pandas import DataFrame as df
fp = '/Users/User/Development/zipcodes/file.csv'
file1 = open(fp, 'rb').read()
df = pd.read_csv(fp, sep=',')

df = df[['VIN', 'Reg Name', 'Reg Address', 'Reg City', 'Reg ST', 'ZIP',
         'ZIP', 'Catagory', 'Phone', 'First Name', 'Last Name', 'Reg NFS',
         'MGVW', 'Make', 'Veh Model','E Mfr', 'Engine Model', 'CY2010',
         'CY2011', 'CY2012', 'CY2013', 'CY2014', 'CY2015', 'Std Cnt', 
        ]]
#reader.head(1)
df.head(1)
zipBlue = [65355, 65350, 65345, 65326, 65335, 64788, 64780, 64777, 64743,
64742, 64739, 64735, 64723, 64722, 64720]

还包含zipGreen, zipRed, zipYellow, ipLightBlue 但未包含在示例中.

Also contains zipGreen, zipRed, zipYellow, ipLightBlue But did not include in example.

def IsInSort():
    blue = df[df.ZIP.isin(zipBlue)]
    green = df[df.ZIP.isin(zipGreen)]
    red = df[df.ZIP.isin(zipRed)]
    yellow = df[df.ZIP.isin(zipYellow)]
    LightBlue = df[df.ZIP.isin(zipLightBlue)]
def SaveSortedZips():
    blue.to_csv('sortedBlue.csv')
    green.to_csv('sortedGreen.csv')
    red.to_csv('sortedRed.csv')
    yellow.to_csv('sortedYellow.csv')
    LightBlue.to_csv('SortedLightBlue.csv')
IsInSort()
SaveSortedZips()

1864#尝试在具有重复1865的轴上重新索引
如果不是self.is_unique和len(indexer): -> 1866提高ValueError(无法从重复的轴重新编制索引")1867 1868 def重新编制索引(自身,目标,方法=无, 级别=无,限制=无):

1864 # trying to reindex on an axis with duplicates 1865
if not self.is_unique and len(indexer): -> 1866 raise ValueError("cannot reindex from a duplicate axis") 1867 1868 def reindex(self, target, method=None, level=None, limit=None):

ValueError:无法从重复的轴重新索引

ValueError: cannot reindex from a duplicate axis

推荐答案

我很确定您的问题与口罩有关

I'm pretty sure your problem is related to your mask

df = df[['VIN', 'Reg Name', 'Reg Address', 'Reg City', 'Reg ST', 'ZIP',
         'ZIP', 'Catagory', 'Phone', 'First Name', 'Last Name', 'Reg NFS',
         'MGVW', 'Make', 'Veh Model','E Mfr', 'Engine Model', 'CY2010',
         'CY2011', 'CY2012', 'CY2013', 'CY2014', 'CY2015', 'Std Cnt', 
        ]]

'ZIP'在其中两次.删除其中之一应该可以解决问题.

'ZIP' is in there twice. Removing one of them should solve the problem.

错误ValueError: cannot reindex from a duplicate axis是这些非常隐秘的熊猫错误之一,它根本无法告诉您错误是什么.

The error ValueError: cannot reindex from a duplicate axis is one of these very very cryptic pandas errors which simply does not tell you what the error is.

该错误通常与在操作之前或之后(内部)将两个列命名为同一列有关.

The error is often related to two columns being named the same either before or after (internally in) the operation.

这篇关于ValueError:无法将isin与pandas一起从重复的轴重新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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