Python 如何删除pandas中某个函数中的筛选行?

Python 如何删除pandas中某个函数中的筛选行?,python,pandas,Python,Pandas,在主文件中,我循环了df_read,我想对行进行排序,并将它们保存在df_read中。因为它有几个相同的行,我在每一行中循环,所以在新的Excel列表(df_write)中可能有几个相同的行。我试图用.drop方法删除双行,但该行仍保留在df_读取文件中。我知道Pandas创建了原始Excel文件的副本。这是否意味着函数(sorted_functions.py)打开自己的副本,main.py打开另一个副本?因此,功能和主要工作在两个不同的副本,而不是在同一个 Main.py df_rea

在主文件中,我循环了df_read,我想对行进行排序,并将它们保存在df_read中。因为它有几个相同的行,我在每一行中循环,所以在新的Excel列表(df_write)中可能有几个相同的行。我试图用.drop方法删除双行,但该行仍保留在df_读取文件中。我知道Pandas创建了原始Excel文件的副本。这是否意味着函数(sorted_functions.py)打开自己的副本,main.py打开另一个副本?因此,功能和主要工作在两个不同的副本,而不是在同一个

Main.py

    df_read.to_excel('Data/Bauteilliste.xlsx')

# set counter back to 0
row_counter = 0
df_sorted_list = []

# for-slice to filter the rows in df_read
# see def sort_functions
for row in df_read:
    if df_read.at[row_counter, 'KZ'] == 'R-R':
            df_sorted = sort_component(df_read, row_counter, 'Wickelfalzrohr', 'L')
            df_sorted_list.append(df_sorted)

            row_counter += 1
    else:
        row_counter += 1

# concat df_sorted_list to df_write
df_write = pd.concat(df_sorted_list, ignore_index=True)

# save new Excel file        
df_write.to_excel('Data/Bauteilliste_sorted.xlsx')

print('end')




Unnamed: 0  Nr  TsNr    LvPos   PosNr   Anz Bez KZ  KennZahl    Art AG  A   B   C   D   E   F   L   W   R   G   H   M   N   X   Y   Ra1Vt   Ra2Vt   Ra3Vt   OF  GW  IsoOf   Bem Bem1    RF  D1  D2  ... L2  L3  L4  L5  Abmessung   Ma  MS  St  IsoArt  IsoZ    Bem2    LtgTyp  Ra1Rl   Ra2Rl   Ra3Rl   OfRoh   AbrArt  ListTyp ContainsIso ContainsFrames  HatEinzelteilzeichnung  Du  Lu  OfRund  OfIsoRund   MaterialListeOf Dmax    MaterialListeAG IsZehnderUPV    IsHovalUPV  OfIsoBauteil    OfOval  OfL90   IfcGlobalId Manufacturer    ArticleNumber   StandardNumber
0   0   NaN NaN NaN NaN 1   Wickelfalzrohr  R-R NaN Längenmaß (m)   NaN NaN NaN NaN 160 NaN NaN 6030    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN Stahlblech verzinkt 0.6 NaN NaN NaN NaN Zuluft  NaN NaN NaN NaN L   L   False   False   False   NaN NaN 3.03    NaN NaN NaN NaN False   False   NaN NaN NaN 2Vw2HO7Vv7wQx4hwLX4DH9  NaN NaN NaN
1   1   NaN NaN NaN NaN 1   Wickelfalzrohr  R-R NaN Längenmaß (m)   NaN NaN NaN NaN 355 NaN NaN 2265    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN Stahlblech verzinkt 0.7 NaN NaN 60.0    NaN Abluft fetthaltig   NaN NaN NaN NaN L   L   True    False   False   NaN NaN 2.53    3.38    NaN NaN NaN False   False   NaN NaN NaN 3XnEXpWHn2egNs8tFyzijO  NaN NaN NaN
2   2   NaN NaN NaN NaN 1   Wickelfalzrohr  R-R NaN Längenmaß (m)   NaN NaN NaN NaN 200 NaN NaN 12332   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN Stahlblech verzinkt 0.6 NaN NaN NaN NaN Abluft  NaN NaN NaN NaN L   L   False   False   False   NaN NaN 7.75    NaN NaN NaN NaN False   False   NaN NaN NaN 07CVFcxTvBBfAws5v4Evmo  NaN NaN NaN
3   3   NaN NaN NaN NaN 1   Wickelfalzrohr  R-R NaN Längenmaß (m)   NaN NaN NaN NaN 100 NaN NaN 2110    NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN Stahlblech verzinkt 0.6 NaN NaN 60.0    NaN Abluft  NaN NaN NaN NaN L   L   True    False   False   NaN NaN 0.66    1.46    NaN NaN NaN False   False   NaN NaN NaN 1g3rQ6SKnDePvXCSoavSyK  NaN NaN NaN
4   4   NaN NaN NaN NaN 1   Wickelfalzrohr  R-R NaN Längenmaß (m)   NaN NaN NaN NaN 125 NaN NaN 785 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN Stahlblech verzinkt 0.6 NaN NaN NaN NaN Abluft  NaN NaN NaN NaN L   L   False   False   False   NaN NaN 0.31    NaN NaN NaN NaN False   False   NaN NaN NaN 2uql6luGz6H9MvAwQofnkf  NaN NaN NaN
排序函数.py

import pandas as pd
def sort_component(df_read, row_counter, description_component, filt_rule):

        # Filter settings
        filt = (df_read['KZ'] == df_read.at[row_counter, 'KZ']) & (df_read['A'] == df_read.at[row_counter, 'A']) & (df_read['B'] == df_read.at[row_counter, 'B']) & (df_read['W'] == df_read.at[row_counter, 'W']) & (df_read['D'] == df_read.at[row_counter, 'D']) & (df_read['D1'] == df_read.at[row_counter, 'D1']) & (df_read['D2'] == df_read.at[row_counter, 'D2']) & (df_read['D3'] == df_read.at[row_counter, 'D3']) & (df_read['IsoArt'] == df_read.at[row_counter, 'IsoArt']) & (df_read['IsoZ'] == df_read.at[row_counter, 'IsoZ']) & (df_read['LtgTyp'] == df_read.at[row_counter, 'LtgTyp'])

        counter_length = 0
        counter_square_meters_isolation = 0

        # for-Slice, get Elements with isolation
        for row in df_read.loc[filt, filt_rule]:
            counter_length += row

        for row in df_read.loc[filt, 'IsoOf']:
            counter_square_meters_isolation += row            

        print(counter_length)
        # create empty List 
        df_sorted = []
        df1 = None
        # append both rows to df3
        df1 = pd.DataFrame([[df_read.at[row_counter, 'Bez'], df_read.at[row_counter, 'KZ'], df_read.at[row_counter, 'A'], df_read.at[row_counter, 'B'], df_read.at[row_counter, 'W'], df_read.at[row_counter, 'D'], df_read.at[row_counter, 'D1'], df_read.at[row_counter, 'D2'], df_read.at[row_counter, 'D3'], df_read.at[row_counter, 'IsoArt'], df_read.at[row_counter, 'IsoZ'], df_read.at[row_counter, 'LtgTyp'], counter_length, counter_square_meters_isolation ]],
                    columns=['Bez', 'KZ', 'A', 'B', 'W', 'D', 'D1', 'D2', 'D3', 'IsoArt', 'IsoZ', 'LtgTyp', 'L', 'Isom²'])

        df_sorted = pd.concat([df1])

        print(df_read.at[row_counter, 'D'])
        df_read = df_read.drop(df_read[(df_read.KZ == df_read.at[row_counter, 'KZ']) & (df_read.A == df_read.at[row_counter, 'A']) & (df_read.B == df_read.at[row_counter, 'B']) & (df_read.W == df_read.at[row_counter, 'W']) & (df_read.D == df_read.at[row_counter, 'D']) & (df_read.D1 == df_read.at[row_counter, 'D1']) & (df_read.D2 == df_read.at[row_counter, 'D2']) & (df_read.D3 == df_read.at[row_counter, 'D3']) & (df_read.IsoArt == df_read.at[row_counter, 'IsoArt']) & (df_read.IsoZ == df_read.at[row_counter, 'IsoZ']) & (df_read.LtgTyp == df_read.at[row_counter, 'LtgTyp'])].index, inplace=True)
        print(df_read)

        return df_sorted



0
160
None
0
355
None
0
200
None
0
100
None
0
125
None
0
100
None
0
200
None
0
250
None
0
160
None
0
160
None
0
200
None
0
200
None
0
125
None
0
200
None
0
160
None
0
125
None
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 16

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-4-03f0f124ecb3> in <module>
     43 # see def sort_functions
     44 for row in df_read:
---> 45     if df_read.at[row_counter, 'KZ'] == 'R-R':
     46             df_sorted = sort_component(df_read, row_counter, 'Wickelfalzrohr', 'L')
     47             df_sorted_list.append(df_sorted)

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   2176 
   2177         key = self._convert_key(key)
-> 2178         return self.obj._get_value(*key, takeable=self._takeable)
   2179 
   2180     def __setitem__(self, key, value):

~\anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index, col, takeable)
   2917         # use positional
   2918         col = self.columns.get_loc(col)
-> 2919         index = self.index.get_loc(index)
   2920         return self._get_value(index, col, takeable=True)
   2921 

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 16
将熊猫作为pd导入
def排序组件(df读取、行计数器、描述组件、过滤规则):
#过滤器设置
(df-u-read(A'A'==df-u-read.在[row_计数器,'KZ''KZ'))和(df-u-read[A'A']==df-u-read.在[row_计数器,KZ'',[KZ'Z''']的一项)和(df-u-u-read读读.在[在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在在=df_read.at[row_counter,'D2'])和(df_read['D3']==df_read.在[row_counter,'D3'])和(df_read['IsoArt']==df_read.在[row_counter,'IsoZ'])和(df read['LtgTyp']==df_read.在[row_counter,'LtgTyp']))
计数器长度=0
计数器\u平方米\u隔离度=0
#对于Slice,获取具有隔离的元素
对于df_read.loc[filt,filt_rule]中的行:
计数器长度+=行
对于df_read.loc[filt,'IsoOf']中的行:
计数器\u平方米\u隔离+=行
打印(计数器长度)
#创建空列表
df_排序=[]
df1=无
#将这两行追加到df3
df1=pd.DataFrame([[df_read.at[row_counter,'Bez'],df_read.at[row_counter,'KZ'],df_read.at[row_counter,'B'],df_read.at[row_counter,'W'],df_read.at[row_counter,'D'],df_read.at[row_counter,'D1','],df_read.at[row_counter,'D2'],df_read.at[row_counter,'D3',"df'],df[row_counter,'IsoZ',df_read.在[row_counter,'LtgTyp',计数器长度,计数器平方米隔离]],
列=['Bez','KZ','A','B','W','D','D1','D2','D3','IsoArt','IsoZ','LtgTyp','L','Isom²'])
df_sorted=pd.concat([df1])
打印(df_读取[行计数器'D']处)
(df-U read.KZ==df-U读读.在[row_计数器,“KZ'”))和(df-U读.A==df-U读.在[row_计数器,“KZ''”)和(df-U读.A==df-U读.在[row_计数器计数器,KZ,“KZ''''',,,,,,)和(df(df-U读.读.读.读.A==df读.读.读.读.读.读.读.A==df.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.读.在[在在在在在在在在在在在在在在在在在在在在在在在在在在在[排排(在在在在在在在在[[row_计数器,'D2'])和(df_read.D3==df_read.at[row_计数器,'D3'])和(df_read.IsoArt==df_read.at[row_计数器,'IsoZ'])和(df_read.LtgTyp==df_read.at[row_计数器,'LtgTyp'])。索引,inplace=True)
打印(df_读取)
返回已排序的dfu
0
160
没有一个
0
355
没有一个
0
200
没有一个
0
100
没有一个
0
125
没有一个
0
100
没有一个
0
200
没有一个
0
250
没有一个
0
160
没有一个
0
160
没有一个
0
200
没有一个
0
200
没有一个
0
125
没有一个
0
200
没有一个
0
160
没有一个
0
125
没有一个
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
get\u loc中的~\anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2645请尝试:
->2646返回自引擎。获取位置(钥匙)
2647键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
关键错误:16
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
43#参见定义排序功能
44对于df_中的行,读取:
--->45如果df_在[row_计数器'KZ']处读取='R-R':
46 df_sorted=排序_组件(df_read,row_counter,'Wickelfalzrohr','L')
47 df_排序列表。追加(df_排序)
~\anaconda3\lib\site packages\pandas\core\index.py in\uuuuu getitem\uuuuuu(self,key)
2176
2177键=自转换键(键)
->2178返回self.obj.\u获取值(*键,takeable=self.\u takeable)
2179
2180定义设置项(自身、键、值):
~\anaconda3\lib\site packages\pandas\core\frame.py in\u get\u值(self、index、col、takeable)
2917#使用位置
2918列=自列。获取位置(列)
->2919索引=自索引获取位置(索引)
2920返回self.\u获取值(索引、列、可取值=True)
2921
get\u loc中的~\anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2646返回自引擎。获取位置(钥匙)
2647键错误除外:
->2648返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2649 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
2650如果indexer.ndim>1或indexer.size>1:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
关键错误:16

如果我将inplace=True改为inplace=False,我没有收到任何错误,但我的新df_write中有几行相同的行

您是否查看了Pandas
删除重复项
以删除相同的行