- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的数据框,遵循这些原则:
a b
0 horse cat
1 dog elephant
运行:
df.loc[:,'a'].apply(lambda x: x.upper())
或
df.loc[:,'b'].apply(lambda x: x.upper())
将相应列中的动物大写。然而,运行
df.loc[:,'a':'b'].apply(lambda x: x.upper())
或
df.loc[:,['a','b']].apply(lambda x: x.upper())
导致“AttributeError: ("'Series' object has no attribute 'upper'", 'occurred at index a')”。
显然,我想知道如何修复它(即能够同时将两列都大写)。但我也想知道一个列如何可以自己拥有属性“upper”,但是当将 lambda 作为多个列的一部分应用于它时会丢失它。
最佳答案
使用 str 访问器:
df.loc[:,'a':'b'].apply(lambda x: x.str.upper())
输出:
a b
0 HORSE CAT
1 DOG ELEPHANT
好吧,让我们做一点调试:
def f(x):
print(type(x))
print(type(x[0]))
df.loc[:,'a':'b'].apply(f)
输出:
<class 'pandas.core.series.Series'>
<class 'str'>
<class 'pandas.core.series.Series'>
<class 'str'>
这里我们使用pd.DataFrame.apply
。
在这种情况下,pandas Series 被传递给函数 f,因此我们可以使用 .str 访问器来调用字符串函数 upper。
现在,让我们来看第一种情况:
def f(x):
print(type(x))
print(type(x[0]))
df.loc[:,'a'].apply(f)
输出:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
在这里,我们正在使用 pd.Series.apply
并传递每个值本身。因此,我们可以简单地直接在每个值上调用字符串函数 upper。
而且,您还可以将 pd.DataFrame.applymap
用作 @chrisz shows在他的解决方案中将数据帧的每个单元格值传递给函数。
关于python - pandas apply upper() 分别作用于两个字符串列中的每一个,但不能一起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50631468/
我想要一个“父”节点,它生成数据并将其传递给工作节点,工作节点将对数据进行必要的计算。 我希望父节点坐下来“监听”工作节点何时告诉他他已准备好接收数据。 实现这一目标的最佳方法是什么?我应该为此使用哪
我想从 c# 写一些 Html(html 是一个例子,这可能是其他语言..) 例如: string div = @" Mon text
由于某种原因,在使用我放置在 XML 中的任何新 View 时,我收到了 nullPointerException。 View 类型(TextView、EditText 等)并不重要。我最初工作的任何
就像在 hibernate 中一样,在 EclipseLink 中,我们有注释 @AdditionalCriteria,它允许我们在数据上添加过滤器。在 hibernate 中,它作为@Filter,
file_get_contents() [function.file-get-contents]: SSL: Connection reset by peer in 上 file_get_conten
我有一个简单的 从某些 JS 中隐藏的标签。 ( display:none )我查看了页面源代码,发现它没有隐藏,但是检查器将其显示为显示:无(内联样式) 为了隔离隐藏 的代码部分,在 JS 代码中
我在 GUI 中工作,我有多种类型的事件可能会导致按钮被禁用。我已经尝试了几种方法来组合这两个 Observable,但我发现的每个解决方案都需要两个 Observable 在产生结果之前发布一个事件
我正在尝试让 jquery 在使用 GWT 时为我做一些 ui 影响。我有添加到页面的通知,点击后应该会消失。由于可能有多个相同类型的通知(警告、错误等),我试图仅在通过 GWT 单击它们时才动态添加
我有一组具有以下结构的复选框: A1 A2 ... 复选框仅用于 UI 控制(不用于表单提交)。我有一个关联的 html 元素,它有一个 onclick
作用于 UDP 套接字时,什么会导致 sendto() 发送的字节数少于请求的字节数? 提出这个问题的动机是找出我需要采取的预防措施,以确保我 始终 在一次调用 sendto() 时收到完整的消息,并
我正在编写一个 postgresql View ,它使用一些复杂的逻辑来生成数据行。它要求我对从查询两个不同表的结果派生的两组数据执行相同的一组步骤。是否可以编写一个概括这些步骤的函数? 例如: CR
我想在按下后更改每个图标的颜色。但是 Expandable Container 中的所有图标在按下其中一个后都会发生变化。 class _ExpandableListViewState extends
我正在动态提取 HTML 内容以在我的站点页面中显示注释。问题是我导入的 HTML 内容在样式标签下嵌入了 CSS,所以它基本上扰乱了我的页面样式。有没有什么办法可以阻止传入的样式作用于我的页面? 例
当我将 stdin FD 状态标志设置为 O_NONBLOCK_fileno, F_GETFL); flag = fcntl(stderr->_fileno, F_GETFL); if(-1
我是一名优秀的程序员,十分优秀!