当前位置: 首页 > 知识库问答 >
问题:

Python Pandas用户警告:排序,因为非串联轴不对齐

公孙新觉
2023-03-14

我正在做一些代码练习,并在获得用户警告的同时应用数据帧合并

/usr/lib64/python2.7/site packages/pandas/core/frame.py:6201:FutureWarning:Sorting,因为非连接轴未对齐。熊猫的未来版本将默认更改为“不排序”。要接受将来的行为,请传递“sort=True”。要保留当前行为并使警告静音,请传递sort=False

在这几行代码中:你能帮助解决这个警告吗?

placement_video = [self.read_sql_vdx_summary, self.read_sql_video_km]
placement_video_summary = reduce(lambda left, right: pd.merge(left, right, on='PLACEMENT', sort=False), placement_video)


placement_by_video = placement_video_summary.loc[:, ["PLACEMENT", "PLACEMENT_NAME", "COST_TYPE", "PRODUCT",
                                                     "VIDEONAME", "VIEW0", "VIEW25", "VIEW50", "VIEW75",
                                                     "VIEW100",
                                                     "ENG0", "ENG25", "ENG50", "ENG75", "ENG100", "DPE0",
                                                     "DPE25",
                                                     "DPE50", "DPE75", "DPE100"]]

# print (placement_by_video)

placement_by_video["Placement# Name"] = placement_by_video[["PLACEMENT",
                                                            "PLACEMENT_NAME"]].apply(lambda x: ".".join(x),
                                                                                     axis=1)

placement_by_video_new = placement_by_video.loc[:,
                         ["PLACEMENT", "Placement# Name", "COST_TYPE", "PRODUCT", "VIDEONAME",
                          "VIEW0", "VIEW25", "VIEW50", "VIEW75", "VIEW100",
                          "ENG0", "ENG25", "ENG50", "ENG75", "ENG100", "DPE0", "DPE25",
                          "DPE50", "DPE75", "DPE100"]]

placement_by_km_video = [placement_by_video_new, self.read_sql_km_for_video]
placement_by_km_video_summary = reduce(lambda left, right: pd.merge(left, right, on=['PLACEMENT', 'PRODUCT'], sort=False),
                                       placement_by_km_video)

#print (list(placement_by_km_video_summary))
#print(placement_by_km_video_summary)
#exit()
# print(placement_by_video_new)
"""Conditions for 25%view"""
mask17 = placement_by_km_video_summary["PRODUCT"].isin(['Display', 'Mobile'])
mask18 = placement_by_km_video_summary["COST_TYPE"].isin(["CPE", "CPM", "CPCV"])
mask19 = placement_by_km_video_summary["PRODUCT"].isin(["InStream"])
mask20 = placement_by_km_video_summary["COST_TYPE"].isin(["CPE", "CPM", "CPE+", "CPCV"])
mask_video_video_completions = placement_by_km_video_summary["COST_TYPE"].isin(["CPCV"])
mask21 = placement_by_km_video_summary["COST_TYPE"].isin(["CPE+"])
mask22 = placement_by_km_video_summary["COST_TYPE"].isin(["CPE", "CPM"])
mask23 = placement_by_km_video_summary["PRODUCT"].isin(['Display', 'Mobile', 'InStream'])
mask24 = placement_by_km_video_summary["COST_TYPE"].isin(["CPE", "CPM", "CPE+"])

choice25video_eng = placement_by_km_video_summary["ENG25"]
choice25video_vwr = placement_by_km_video_summary["VIEW25"]
choice25video_deep = placement_by_km_video_summary["DPE25"]

placement_by_km_video_summary["25_pc_video"] = np.select([mask17 & mask18, mask19 & mask20, mask17 & mask21],
                                                  [choice25video_eng, choice25video_vwr, choice25video_deep])


"""Conditions for 50%view"""
choice50video_eng = placement_by_km_video_summary["ENG50"]
choice50video_vwr = placement_by_km_video_summary["VIEW50"]
choice50video_deep = placement_by_km_video_summary["DPE50"]

placement_by_km_video_summary["50_pc_video"] = np.select([mask17 & mask18, mask19 & mask20, mask17 & mask21],
                                                  [choice50video_eng,
                                                   choice50video_vwr, choice50video_deep])

"""Conditions for 75%view"""

choice75video_eng = placement_by_km_video_summary["ENG75"]
choice75video_vwr = placement_by_km_video_summary["VIEW75"]
choice75video_deep = placement_by_km_video_summary["DPE75"]

placement_by_km_video_summary["75_pc_video"] = np.select([mask17 & mask18, mask19 & mask20, mask17 & mask21],
                                                  [choice75video_eng,
                                                   choice75video_vwr,
                                                   choice75video_deep])

"""Conditions for 100%view"""

choice100video_eng = placement_by_km_video_summary["ENG100"]
choice100video_vwr = placement_by_km_video_summary["VIEW100"]
choice100video_deep = placement_by_km_video_summary["DPE100"]
choicecompletions = placement_by_km_video_summary['COMPLETIONS']

placement_by_km_video_summary["100_pc_video"] = np.select([mask17 & mask22, mask19 & mask24, mask17 & mask21, mask23 & mask_video_video_completions],
                                                          [choice100video_eng, choice100video_vwr, choice100video_deep, choicecompletions])



"""conditions for 0%view"""

choice0video_eng = placement_by_km_video_summary["ENG0"]
choice0video_vwr = placement_by_km_video_summary["VIEW0"]
choice0video_deep = placement_by_km_video_summary["DPE0"]

placement_by_km_video_summary["Views"] = np.select([mask17 & mask18, mask19 & mask20, mask17 & mask21],
                                                   [choice0video_eng,
                                                    choice0video_vwr,
                                                    choice0video_deep])


#print (placement_by_km_video_summary)
#exit()

#final Table

placement_by_video_summary = placement_by_km_video_summary.loc[:,
                             ["PLACEMENT", "Placement# Name", "PRODUCT", "VIDEONAME", "COST_TYPE",
                              "Views", "25_pc_video", "50_pc_video", "75_pc_video","100_pc_video",
                              "ENGAGEMENTS","IMPRESSIONS", "DPEENGAMENTS"]]

#placement_by_km_video = [placement_by_video_summary, self.read_sql_km_for_video]
#placement_by_km_video_summary = reduce(lambda left, right: pd.merge(left, right, on=['PLACEMENT', 'PRODUCT']),
                                       #placement_by_km_video)


#print(placement_by_video_summary)
#exit()
# dup_col =["IMPRESSIONS","ENGAGEMENTS","DPEENGAMENTS"]

# placement_by_video_summary.loc[placement_by_video_summary.duplicated(dup_col),dup_col] = np.nan

# print ("Dhar",placement_by_video_summary)

'''adding views based on conditions'''
#filter maximum value from videos

placement_by_video_summary_new = placement_by_km_video_summary.loc[
    placement_by_km_video_summary.reset_index().groupby(['PLACEMENT', 'PRODUCT'])['Views'].idxmax()]
#print (placement_by_video_summary_new)
#exit()
# print (placement_by_video_summary_new)
# mask22 = (placement_by_video_summary_new.PRODUCT.str.upper ()=='DISPLAY') & (placement_by_video_summary_new.COST_TYPE=='CPE')

placement_by_video_summary_new.loc[mask17 & mask18, 'Views'] = placement_by_video_summary_new['ENGAGEMENTS']
placement_by_video_summary_new.loc[mask19 & mask20, 'Views'] = placement_by_video_summary_new['IMPRESSIONS']
placement_by_video_summary_new.loc[mask17 & mask21, 'Views'] = placement_by_video_summary_new['DPEENGAMENTS']

#print (placement_by_video_summary_new)
#exit()
placement_by_video_summary = placement_by_video_summary.drop(placement_by_video_summary_new.index).append(
    placement_by_video_summary_new).sort_index()

placement_by_video_summary["Video Completion Rate"] = placement_by_video_summary["100_pc_video"] / \
                                                      placement_by_video_summary["Views"]

placement_by_video_final = placement_by_video_summary.loc[:,
                           ["Placement# Name", "PRODUCT", "VIDEONAME", "Views",
                            "25_pc_video", "50_pc_video", "75_pc_video", "100_pc_video",
                            "Video Completion Rate"]]

共有2个答案

唐炜
2023-03-14

Jezrael的回答很好,但没有回答我的一个问题:错误地获得“排序”标志会以任何方式扰乱我的数据吗?答案显然是“不”,不管怎样你都很好。

from pandas import DataFrame, concat

a = DataFrame([{'a':1,      'c':2,'d':3      }])
b = DataFrame([{'a':4,'b':5,      'd':6,'e':7}])

>>> concat([a,b],sort=False)
   a    c  d    b    e
0  1  2.0  3  NaN  NaN
0  4  NaN  6  5.0  7.0

>>> concat([a,b],sort=True)
   a    b    c  d    e
0  1  NaN  2.0  3  NaN
0  4  5.0  NaN  6  7.0
颜高格
2023-03-14

tl;博士:

concatappend如果列不匹配,则当前对非连接索引(例如,如果添加行,则为列)进行排序。在0.23,这开始产生警告;传递参数sort=True使其静音。在将来,默认值将更改为不排序,因此最好现在指定sort=TrueFalse,或者更好地确保非串联索引匹配。

该警告在0.23.0中是新的:

在熊猫的未来版本中pandas.concat()DataFrame.append()将不再对未对齐的非连接轴进行排序。当前行为与前一个相同(排序),但现在在未指定排序且未对齐非串联轴时发出警告,链接。

更多信息来自链接的非常古老的github问题,评论者smCinerney:

当连接数据帧时,如果列名之间存在任何差异,则它们将按字母数字排序。如果它们在数据帧中是相同的,则不会进行排序。

这种类型是没有记录和不需要的。当然,默认行为应该是无排序的。

一段时间后,参数排序pandas.concatDataFrame.append中实现:

排序:布尔值,默认无

如果连接为“外部”时未对齐,则对非连接轴进行排序。当前默认的排序已弃用,在未来版本的pandas中将更改为不排序。

显式传递sort=True以使警告和排序静音。显式传递sort=False以使警告静音,而不进行排序。

这在连接='内部'时没有影响,因为它已经保留了非连接轴的顺序。

因此,如果两个DataFrames以相同的顺序具有相同的列,则没有警告和排序:

df1 = pd.DataFrame({"a": [1, 2], "b": [0, 8]}, columns=['a', 'b'])
df2 = pd.DataFrame({"a": [4, 5], "b": [7, 3]}, columns=['a', 'b'])

print (pd.concat([df1, df2]))
   a  b
0  1  0
1  2  8
0  4  7
1  5  3

df1 = pd.DataFrame({"a": [1, 2], "b": [0, 8]}, columns=['b', 'a'])
df2 = pd.DataFrame({"a": [4, 5], "b": [7, 3]}, columns=['b', 'a'])

print (pd.concat([df1, df2]))
   b  a
0  0  1
1  8  2
0  7  4
1  3  5

但是,如果数据帧具有不同的列,或者相同的列具有不同的顺序,则如果未显式设置参数sortsort=None是默认值),pandas将返回警告:

df1 = pd.DataFrame({"a": [1, 2], "b": [0, 8]}, columns=['b', 'a'])
df2 = pd.DataFrame({"a": [4, 5], "b": [7, 3]}, columns=['a', 'b'])

print (pd.concat([df1, df2]))

FutureWarning:排序,因为非连接轴未对齐。

   a  b
0  1  0
1  2  8
0  4  7
1  5  3

print (pd.concat([df1, df2], sort=True))
   a  b
0  1  0
1  2  8
0  4  7
1  5  3

print (pd.concat([df1, df2], sort=False))
   b  a
0  0  1
1  8  2
0  7  4
1  3  5

如果数据帧有不同的列,但第一列是对齐的-它们将被正确地分配给彼此(在下面的示例中,来自df1的列ab以及来自df2的列b),因为它们都存在于这两个列中。对于存在于一个但不是两个数据帧中的其他列,将创建缺少的值。

最后,如果传递sort=True,则列按字母数字排序。如果sort=False并且第二个DafaFrame包含不在第一个DafaFrame中的列,则会将它们附加到末尾,而不进行排序:

df1 = pd.DataFrame({"a": [1, 2], "b": [0, 8], 'e':[5, 0]}, 
                    columns=['b', 'a','e'])
df2 = pd.DataFrame({"a": [4, 5], "b": [7, 3], 'c':[2, 8], 'd':[7, 0]}, 
                    columns=['c','b','a','d'])

print (pd.concat([df1, df2]))

FutureWarning:排序,因为非连接轴未对齐。

   a  b    c    d    e
0  1  0  NaN  NaN  5.0
1  2  8  NaN  NaN  0.0
0  4  7  2.0  7.0  NaN
1  5  3  8.0  0.0  NaN

print (pd.concat([df1, df2], sort=True))
   a  b    c    d    e
0  1  0  NaN  NaN  5.0
1  2  8  NaN  NaN  0.0
0  4  7  2.0  7.0  NaN
1  5  3  8.0  0.0  NaN

print (pd.concat([df1, df2], sort=False))

   b  a    e    c    d
0  0  1  5.0  NaN  NaN
1  8  2  0.0  NaN  NaN
0  7  4  NaN  2.0  7.0
1  3  5  NaN  8.0  0.0

在您的代码中:

placement_by_video_summary = placement_by_video_summary.drop(placement_by_video_summary_new.index)
                                                       .append(placement_by_video_summary_new, sort=True)
                                                       .sort_index()
 类似资料:
  • 问题内容: 我正在做一些代码练习,并在收到用户警告的同时应用数据帧合并 /usr/lib64/python2.7/site- packages/pandas/core/frame.py:6201:FutureWarning:排序是因为未串联的轴未对齐。熊猫的未来版本将更改为默认情况下不排序。要接受将来的行为,请传递“ sort = True”。要保留当前行为并消除警告,请传递sort = Fals

  • 问题内容: 我想打印所有可用语言的排序波兰语名称。 不幸的是我对排序部分有疑问。输出为: 不幸的是,波兰语的出现前后,因此输出应为: 我该怎么做?是否存在一种通用的非语言相关的方法(例如,我现在要显示此方法,并使用另一种排序规则以另一种语言进行排序)。 问题答案: 您应该将整理器传递给sort方法: 默认的排序顺序是由字符串中的Unicode代码点定义的,并且在任何语言中都不是正确的字母顺序。

  • 问题内容: 将我的php版本更新到5.4.0-3后,我收到一个奇怪的PHP错误。 我有这个数组: 当我尝试像这样访问它时,会收到奇怪的警告 我真的不想只编辑我的php.ini并重新设置错误级别。 问题答案: 请尝试这种方式。…我已经测试了此代码。

  • 我试图用PHP做一个待办事项列表 但是我收到这个错误警告:非法的字符串偏移量'todo' 在我的todo.php页面的顶部 在html部分 我该怎么解决这个?它一直警告我=>警告:非法字符串偏移量'todo' todo存在于我的mysql数据库中,其中包含待办事项activity的数据

  • 问题内容: 最近更新到PHP 7.1,并开始出现以下错误 警告:第29行遇到非数字值 这是第29行的样子 在本地主机上一切正常。 任何想法如何解决这个问题或它是什么? 问题答案: 似乎在PHP 7.1中,如果遇到非数字值,将发出警告。请参阅此链接。 这是与您收到的警告通知有关的相关部分: 当使用期望数字或它们的赋值等效项的运算符强制无效字符串时,将引入新的E_WARNING和E_NOTICE错误。

  • 在我看来最好的情况应该是O(log n),但不确定最坏的情况...也许O(n^2)一次只能匹配一个字母。 谁能给我更多的点子吗?