首页 > 解决方案 > 无法使用 pandas iloc 摆脱列标签

问题描述

我有一个大的 csv(15 行和 2500 列),并试图将每一行中的值与上面的行进行比较。为此,我使用ilocpandas 将每一行拆分为自己的数据框。所以现在我有 15 个数据帧,我可以尝试使用compare(). 问题是我不断收到错误Can only compare identically-labeled DataFrame objects,但是当将每个数据框放入 csv 时,它们都被标记为相同的东西,所以我想如果我摆脱了可以工作的标签并尝试了这个并且没有用,所以我尝试排序此处显示的索引,我仍然得到相同的错误。CSV 中主要填充了浮点数和偶尔的 NaN 值以表示其价值。

我将其拆分为iloc使用df_i = df.iloc[[i]]wherei替换为 1-14 以将每一行作为数据框。

打印数据帧给了我这个输出:

                        TIME  EVENT  Unld1Comp1Circ2_Dout.Val  ...  WorkingHours.Start_Count_14.Cnt  WorkingHours.Start_Count_15.Cnt  WorkingHours.Start_Count_16.Cnt
1  2020-12-15T17:23:55+01:00    NaN                         1  ...                                0                                0                                0

[1 rows x 2463 columns] 
                         TIME  EVENT  Unld1Comp1Circ2_Dout.Val  ...  WorkingHours.Start_Count_14.Cnt  WorkingHours.Start_Count_15.Cnt  WorkingHours.Start_Count_16.Cnt
2  2020-12-15T17:24:13+01:00    NaN                         1  ...                                0                                0                                0

[1 rows x 2463 columns]

标签: pythonpandasdataframe

解决方案


有几种方法可以找出数据框中两行之间的差异。

选项1:

for col in df.columns[2:]: #check column by column from 3rd column thru the end

    if df[col].nunique() > 1:
        print (col, df[col].unique())

这将打印所有具有不同值的列。如果nunique()大于 1,则该列中存在多个值。但是,这不会告诉您哪一行有差异。

选项 2:

另一种方法是执行 adf[col].shift()并与前一行进行比较。如果有差异,请密切关注。对每一行进行相同的比较。合并所有差异,您将获得所有行的列表,这些行在下一行之间至少有一个值不同。

为此,您可以执行以下操作:

import pandas as pd
df = pd.DataFrame({'col1':[1,1,1,1,1,1,1,1],
                   'col2':[2,2,2,2,2,2,2,2],
                   'col3':[3,3,3,3,3,3,3,4],
                   'col4':[4,4,4,4,4,4,5,4],
                   'col5':[5,5,5,5,5,1,5,5]})
print (df)

df['Differs'] = False #set all rows to matched 

for col in df.columns[2:]: #check column by column from 3rd column thru the end

    #if df[col].nunique() > 1:
        #print (col, df[col].unique())

    df['newcol1'] = df[col].shift() != df[col] #check against next row. True if differs
    df.loc[:0,'newcol1'] = False # Tweak the first row as it should be ignored
    
    df.loc[df['newcol1'] == True,'Differs'] = True #if any row matched, set Differs to True

print (df[df['Differs']]) #print all rows that has a different value in at least one column 

在上面的示例中,第 3、4、5 行至少有一个与前一行不同的值。

对于以下给定的数据框:

   col1  col2  col3  col4  col5
0     1     2     3     4     5
1     1     2     3     4     5
2     1     2     3     4     5
3     1     2     3     4     5
4     1     2     3     4     5
5     1     2     3     4     1
6     1     2     3     5     5
7     1     2     4     4     5

输出将是:

   col1  col2  col3  col4  col5  Differs  newcol1
5     1     2     3     4     1     True     True
6     1     2     3     5     5     True    False
7     1     2     4     4     5     True    False

使用这两种技术,我进行了比较。

选项 1 结果:

所有这些列都有一个以上的值。这些值位于列名称旁边的列表中。

AFreezeSetP [-39.666669 -39.333334]
AFreezeUserT_1K [23 19]
UserPmp1_On [0 1]
RunTempRegKp [3.111111 3.444445]
RunTempRegTi [399 398]
RunTempRegTd [99 96]
RegSetP [-27.333334 -26.888891]
CoolSetP [-27.333334 -26.888891]
AFreezeUserDiff [29.833334 29.777778]
AFreezeDiff [2.       1.944445]
W_OutTempUserPrb.Val [-24.488002 -24.478   ]
DscgP_Prb_Circ1.Val [-4.287679 -4.291988]
W_OutTempUser [-24.488002 -24.478   ]
DscgP_Circ1 [-4.287679 -4.291988]
SuctTempCirc1 [-174.8 -174.7]
RegTypStartup [1 2]
RegTypRun [1 0]
SuctSH_Circ1 [225.2 225.3]
UserPmp1_Dout.Val [0 1]
UserPmp1_Aout.Val [  0. 100.]
UserPmp1HrsThrsh [4377 4378]
HiW_TempStartupDT [59 55]
HiW_TempRunDT [181 186]
HiW_TempOfs [11.166667 11.444445]
DscgP_Circ2 [-4.287679 -4.291988]
SuctTempCirc2 [-174.8 -174.7]
DscgP_Prb_Circ2.Val [-4.287679 -4.291988]
SuctSH_Circ2 [225.2 225.3]
WorkingHours.UserPmp1Starts [0 1]
W_UserTempReg [-24.488002 -24.478    -78.805   ]
At_SP_Dout.Val [0 1]
SonicDensitySensor.SonicDensity_1.EnSensor [0 1]

选项 2结果是除第 1 行之外的所有行。这告诉我每行(1 到 15)之间至少有一个不同的值。您可以调整我的代码以找出每列不同的特定行和列。

                         TIME  EVENT  ...  Differs  newcol1
1   2020-12-15T17:23:55+01:00    NaN  ...     True     True
2   2020-12-15T17:24:13+01:00    NaN  ...     True    False
3   2020-12-15T17:24:24+01:00    NaN  ...     True    False
4   2020-12-15T17:24:26+01:00    NaN  ...     True    False
5   2020-12-15T17:24:29+01:00    NaN  ...     True    False
6   2020-12-15T17:24:32+01:00    NaN  ...     True    False
7   2020-12-15T17:24:35+01:00    NaN  ...     True    False
8   2020-12-15T17:24:40+01:00    NaN  ...     True    False
9   2020-12-15T17:24:42+01:00    NaN  ...     True    False
10  2020-12-15T17:24:43+01:00    NaN  ...     True    False
11  2020-12-15T17:24:53+01:00    NaN  ...     True    False
12  2020-12-15T17:24:55+01:00    NaN  ...     True    False
13  2020-12-15T17:25:01+01:00    NaN  ...     True    False
14  2020-12-15T17:25:02+01:00    NaN  ...     True    False

推荐阅读