创建基于另一个的新列而不会引发警告 [英] Creating a new column based on another without throwing warning

查看:73
本文介绍了创建基于另一个的新列而不会引发警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dataFrame live(活产),其列为"agepreg",它是一个浮点数,保留两位小数.我想创建一个新列'agepreg_rounded'作为整数.

I have dataFrame live (live births) with a column 'agepreg' which is a float with two decimal places. I'd like to create a new column 'agepreg_rounded' as integer.

我幼稚的方法:

live['agepreg_rounded'] = live['agepreg'].apply(lambda x: round(x,0))

可以工作,但会发出警告:

Does work, but throws warning:

/usr/local/lib/python3.5/dist-packages/ipykernel/__main__.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

我已经尝试过多次使用.loc语法,但是失败了.

I've tried multiple times to use the .loc syntax, but failed.

有人可以让我挺直吗?

这是我想写的那种东西,但这显然是错误的:

Here's the sort of thing I'm tempted to write, but that is clearly wrong:

live['agepreg_rounded'] = live.loc[live['agepreg']].apply(lambda x: round(x,0))

更新:住所来自哪里?

我正在跟踪O'Reilly的ThinkStats2书籍,数据来自与源材料一起下载的文件中

I'm following ThinkStats2 book from O'Reilly and the data comes from a file downloaded with the source material:

import nsfg
preg = nsfg.ReadFemPreg()
live = preg[preg.outcome == 1]

推荐答案

我认为您需要

I think you need copy and then instead apply use Series.round:

live = preg[preg.outcome == 1].copy()
live['agepreg_rounded'] = live['agepreg'].round(0)

如果以后在live中修改值,您会发现修改不会传播回原始数据(preg),并且Pandas会发出警告.

If you modify values in live later you will find that the modifications do not propagate back to the original data (preg), and that Pandas does warning.

这篇关于创建基于另一个的新列而不会引发警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆