1. <i id='gP0wi'><tr id='gP0wi'><dt id='gP0wi'><q id='gP0wi'><span id='gP0wi'><b id='gP0wi'><form id='gP0wi'><ins id='gP0wi'></ins><ul id='gP0wi'></ul><sub id='gP0wi'></sub></form><legend id='gP0wi'></legend><bdo id='gP0wi'><pre id='gP0wi'><center id='gP0wi'></center></pre></bdo></b><th id='gP0wi'></th></span></q></dt></tr></i><div id='gP0wi'><tfoot id='gP0wi'></tfoot><dl id='gP0wi'><fieldset id='gP0wi'></fieldset></dl></div>
        <bdo id='gP0wi'></bdo><ul id='gP0wi'></ul>

    2. <tfoot id='gP0wi'></tfoot><legend id='gP0wi'><style id='gP0wi'><dir id='gP0wi'><q id='gP0wi'></q></dir></style></legend>

      <small id='gP0wi'></small><noframes id='gP0wi'>

        在 Pandas DataFrame 中评估 if-then-else 块中的多个条件

        时间:2023-08-30

              <legend id='kkieI'><style id='kkieI'><dir id='kkieI'><q id='kkieI'></q></dir></style></legend>

                <tbody id='kkieI'></tbody>
            • <i id='kkieI'><tr id='kkieI'><dt id='kkieI'><q id='kkieI'><span id='kkieI'><b id='kkieI'><form id='kkieI'><ins id='kkieI'></ins><ul id='kkieI'></ul><sub id='kkieI'></sub></form><legend id='kkieI'></legend><bdo id='kkieI'><pre id='kkieI'><center id='kkieI'></center></pre></bdo></b><th id='kkieI'></th></span></q></dt></tr></i><div id='kkieI'><tfoot id='kkieI'></tfoot><dl id='kkieI'><fieldset id='kkieI'></fieldset></dl></div>

              <small id='kkieI'></small><noframes id='kkieI'>

                  <bdo id='kkieI'></bdo><ul id='kkieI'></ul>
                  <tfoot id='kkieI'></tfoot>
                  本文介绍了在 Pandas DataFrame 中评估 if-then-else 块中的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想通过在 if-then-else 块中评估多个条件来在 Pandas DataFrame 中创建一个新列.

                  如果 events.hour <= 6:事件['time_slice'] = '晚上'elif events.hour <= 12:事件['time_slice'] = '早上'elif events.hour <= 18:事件['time_slice'] = '下午'elif events.hour <= 23:事件['time_slice'] = '晚上'

                  当我运行它时,我收到以下错误:

                  <块引用>

                  ValueError:Series 的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all().

                  所以我尝试通过添加如下所示的任何语句来解决这个问题:

                  if (events.hour <= 6).any():事件['time_slice'] = '晚上'elif (events.hour <= 12).any():事件['time_slice'] = '早上'elif (events.hour <= 18).any():事件['time_slice'] = '下午'elif (events.hour <= 23).any():事件['time_slice'] = '晚上'

                  现在我没有收到任何错误,但是当我检查 time_slice 的唯一值时,它只显示 'night'

                  np.unique(events.time_slice)

                  <块引用>

                  array(['night'], dtype=object)

                  我该如何解决这个问题,因为我的数据包含应该是早上"、下午"或晚上"的样本.谢谢!

                  解决方案

                  你可以使用 pd.cut() 方法来对您的数据进行分类:

                  演示:

                  在 [66]: events = pd.DataFrame(np.random.randint(0, 23, 10), columns=['hour'])在 [67] 中:事件出[67]:小时0 51 172 123 24 205 226 207 118 149 8在 [71] 中: events['time_slice'] = pd.cut(events.hour, bins=[-1, 6, 12, 18, 23], labels=['night','morning','afternoon','晚上'])在 [72] 中:事件出[72]:小时时间片0 5 晚1月17日下午2 12 上午3 2 晚4 20 晚5月22日晚6月20日晚上7 月 11 日上午8月14日下午9 8 上午

                  I want to create a new column in a Pandas DataFrame by evaluating multiple conditions in an if-then-else block.

                  if events.hour <= 6:
                      events['time_slice'] = 'night'
                  elif events.hour <= 12:
                      events['time_slice'] = 'morning'
                  elif events.hour <= 18:
                      events['time_slice'] = 'afternoon'
                  elif events.hour <= 23:
                      events['time_slice'] = 'evening'
                  

                  When I run this, I get the error below:

                  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

                  So I tried to solve this by adding the any statement like shown below:

                  if (events.hour <= 6).any():
                      events['time_slice'] = 'night'
                  elif (events.hour <= 12).any():
                      events['time_slice'] = 'morning'
                  elif (events.hour <= 18).any():
                      events['time_slice'] = 'afternoon'
                  elif (events.hour <= 23).any():
                      events['time_slice'] = 'evening'
                  

                  Now I do not get any error, but when I check the unique values of time_slice, it only shows 'night'

                  np.unique(events.time_slice)
                  

                  array(['night'], dtype=object)

                  How can I solve this, because my data contains samples that should get 'morning', 'afternoon' or 'evening'. Thanks!

                  解决方案

                  you can use pd.cut() method in order to categorize your data:

                  Demo:

                  In [66]: events = pd.DataFrame(np.random.randint(0, 23, 10), columns=['hour'])
                  
                  In [67]: events
                  Out[67]:
                     hour
                  0     5
                  1    17
                  2    12
                  3     2
                  4    20
                  5    22
                  6    20
                  7    11
                  8    14
                  9     8
                  
                  In [71]: events['time_slice'] = pd.cut(events.hour, bins=[-1, 6, 12, 18, 23], labels=['night','morning','afternoon','evening'])
                  
                  In [72]: events
                  Out[72]:
                     hour time_slice
                  0     5      night
                  1    17  afternoon
                  2    12    morning
                  3     2      night
                  4    20    evening
                  5    22    evening
                  6    20    evening
                  7    11    morning
                  8    14  afternoon
                  9     8    morning
                  

                  这篇关于在 Pandas DataFrame 中评估 if-then-else 块中的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:while 语句后的 Else 语句,以及字符串与字符串列表的比较? 下一篇:Python中带有两个条件的if语句

                  相关文章

                • <i id='qy3Bi'><tr id='qy3Bi'><dt id='qy3Bi'><q id='qy3Bi'><span id='qy3Bi'><b id='qy3Bi'><form id='qy3Bi'><ins id='qy3Bi'></ins><ul id='qy3Bi'></ul><sub id='qy3Bi'></sub></form><legend id='qy3Bi'></legend><bdo id='qy3Bi'><pre id='qy3Bi'><center id='qy3Bi'></center></pre></bdo></b><th id='qy3Bi'></th></span></q></dt></tr></i><div id='qy3Bi'><tfoot id='qy3Bi'></tfoot><dl id='qy3Bi'><fieldset id='qy3Bi'></fieldset></dl></div>

                  • <bdo id='qy3Bi'></bdo><ul id='qy3Bi'></ul>

                • <small id='qy3Bi'></small><noframes id='qy3Bi'>

                  <tfoot id='qy3Bi'></tfoot>

                    1. <legend id='qy3Bi'><style id='qy3Bi'><dir id='qy3Bi'><q id='qy3Bi'></q></dir></style></legend>