首页 \ 问答 \ 大熊猫指定一个用notnull()过滤的系列,(Pandas Assigning back a series that was filtered with notnull())

大熊猫指定一个用notnull()过滤的系列,(Pandas Assigning back a series that was filtered with notnull())

各位开发人员,

我目前正在学习大熊猫,并且仍然试图围绕如何分配值,我将其转换为特定类型,例如回到原始数据框。

更具体的我有这个数据框:

     id         A       B
0   50000    12413     32885.0
1   50001     2040     43737.0
3   50002     2040     28015.0
4   50003     2040      NaN
5   50004     2040     28565.0

我的目标是将B列转换为整数,但保留NaN值,因此没有fillna(0) 。 我想要这个:

     id         A       B
0   50000    12413     32885
1   50001     2040     43737
3   50002     2040     28015
4   50003     2040      NaN
5   50004     2040     28565

我用print(df.loc[df['B'].notnull(), 'B'].astype('int'))做了这个print(df.loc[df['B'].notnull(), 'B'].astype('int'))并且它有效。

        B
0   32885
1   43737
3   28015
4   28565

但是,如果我尝试将其分配回数据帧蒙山:

df.loc[df['B'].notnull(), 'B'] = df.loc[df['B'].notnull(), 'B'].astype('int')

我仍然得到原始的,未转换的数据。 所以我似乎做错了任务,但我无法弄清楚如何正确地做到这一点。 帮助将非常感谢!


Hello fellow developers,

I am currently learning pandas and still trying to wrap my head around how to assign values, that I converted to a specific type for example, back to the origin dataframe.

More specific I have this dataframe:

     id         A       B
0   50000    12413     32885.0
1   50001     2040     43737.0
3   50002     2040     28015.0
4   50003     2040      NaN
5   50004     2040     28565.0

My goal is to convert column B to integers but leave NaN values as is, so no fillna(0). I want to have this:

     id         A       B
0   50000    12413     32885
1   50001     2040     43737
3   50002     2040     28015
4   50003     2040      NaN
5   50004     2040     28565

I did this with print(df.loc[df['B'].notnull(), 'B'].astype('int')) and it worked.

        B
0   32885
1   43737
3   28015
4   28565

But if I try to assign it back to the data frame whith:

df.loc[df['B'].notnull(), 'B'] = df.loc[df['B'].notnull(), 'B'].astype('int')

I still get the original, unconverted data. So I seem to do the assignment wrong, but I can't figure out how to do it correctly. Help would be much appreciated!


原文:https://stackoverflow.com/questions/50603794
更新时间:2021-12-18 19:12

最满意答案

我相信你的问题在标题部分。 我认为问题在于它周围缺少引号。

基本上,你需要确保

@(tecDoc.Text + " " + tecDoc.Value) = "Some text"

在Chrome上右键单击它并按“检查元素”以查看当前的外观。

另外,我建议添加数据放置标记。 只是为了确保它在各处看起来都一样:)

本来只是评论而不是回答,但所以不会让我..祝你好运! :)


I believe your issue is in the title section. I think the issue is the fact that it's lacking quotation marks around it.

basically, you need to make sure that

@(tecDoc.Text + " " + tecDoc.Value) = "Some text"

Right click it on Chrome and press "inspect element" to see what that currently looks like.

Also I suggest adding a data placement tag. Just to be sure it will look the same everywhere :)

Would have just commented instead of answering, but SO wont let me.. Good luck! :)

相关问答

更多
  • 您可以使用引导Tooltip Option trigger来处理这种情况。 如何触发工具提示 - 点击| 悬停| 重点| 手册。 你可能会传递多个触发器; 用空格分隔它们。 手动不能与任何其他触发器结合使用。 更多信息请阅读此处 点击这里查看工作演示 JS: $('[data-toggle="tooltip"]').tooltip({ trigger: "hover" }); You can use bootstrap Tooltip Option trigger to handle this ...
  • 好吧,问题是其他地方。 无论如何,现在我使用这个,因为这会更改所有需要工具提示的组件,其中上面的组件只更改一个组件。 $(".track a").tooltip('hide') .attr('data-original-title', time) .tooltip('fixTitle') .tooltip('show'); 初始化工具提示时,您必须将动画设置为false: $(".track a").tooltip({ 'animation' : false }); ...
  • 使用fixTitle选项更新工具提示。 $("#addRssbtn").attr("title", "Remove content from Personal RSS") 变 $("#addRssbtn").attr("title", "Remove content from Personal RSS").tooltip('fixTitle'); 您可以添加.tooltip('show'); 以后直接显示工具提示: $("#addRssbtn").attr("title", "Remove conten ...
  • 在td中添加跨度并在跨度上提供工具提示。 那么如果跨度中没有内容 - 隐藏它。 另外请注意,您需要为表格添加唯一的ID,这样如果您有多于一行的结构,可能需要修改#cost。 @Html.DisplayFor(modelItem => item.Data.ProductId) @Html.DisplayFor(modelItem => item.Data.Product ...
  • $(document).ready(function(){ $('[data-toggle="tooltip"]').each(function(){ var url=$(this).prop('href'); $(this).tooltip({ html:true, title: $(url.substr(url.lastIndexOf('#'))).html() }) }) }); /* Tooltip */ .tooltip ...
  • 看来,你还没有包括jquery和所需的引导JavaScript文件 。 样式表和JavaScript的位置也很重要。 尝试将引导JavaScript文件和引导样式表文件放在其他样式表和JavaScript文件的末尾。 注意: id是唯一的! 使用class或data-toggle作为选择器是明智的。 $(function() { $('[data-toggle="tooltip"]').tooltip() });
    我相信你的问题在标题部分。 我认为问题在于它周围缺少引号。 基本上,你需要确保 @(tecDoc.Text + " " + tecDoc.Value) = "Some text" 在Chrome上右键单击它并按“检查元素”以查看当前的外观。 另外,我建议添加数据放置标记。 只是为了确保它在各处看起来都一样:) 本来只是评论而不是回答,但所以不会让我..祝你好运! :) I believe your issue is in the title section. I think the issue is th ...
  • 选择加入功能 出于性能原因,Tooltip和Popover data-apis是选择加入的,这意味着您必须自己初始化它们。 初始化页面上所有工具提示的一种方法是通过数据切换属性选择它们:复制 $(function () { $('[data-toggle="tooltip"]').tooltip() }) (来源: Bootstrap-Tabs ) 因此,您可以在关闭正文标记之前将其添加到html文件中: