- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有年龄的数据框 df,我正在努力将文件分类为 0 和 1 的年龄组。
df:
User_ID | Age
35435 22
45345 36
63456 18
63523 55
我尝试了以下方法
df['Age_GroupA'] = 0
df['Age_GroupA'][(df['Age'] >= 1) & (df['Age'] <= 25)] = 1
但是得到这个错误
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
为了避免它,我要使用 .loc
df['Age_GroupA'] = 0
df['Age_GroupA'] = df.loc[(df['Age'] >= 1) & (df['Age'] <= 25)] = 1
但是,这会将所有年龄标记为 1
这是我得到的
User_ID | Age | Age_GroupA
35435 22 1
45345 36 1
63456 18 1
63523 55 1
虽然这是目标
User_ID | Age | Age_GroupA
35435 22 1
45345 36 0
63456 18 1
63523 55 0
谢谢
最佳答案
由于同行压力 (@DSM),我觉得有必要分解您的错误:
df['Age_GroupA'][(df['Age'] >= 1) & (df['Age'] <= 25)] = 1
这是 chained indexing/assignment
接下来你尝试了什么:
df['Age_GroupA'] = df.loc[(df['Age'] >= 1) & (df['Age'] <= 25)] = 1
是不正确的形式,当使用 loc
时你想要:
df.loc[<boolean mask>, cols of interest] = some scalar or calculated value
像这样:
df.loc[(df['Age_MDB_S'] >= 1) & (df['Age_MDB_S'] <= 25), 'Age_GroupA'] = 1
您也可以使用 np.where
完成此操作:
df['Age_GroupA'] = np.where( (df['Age_MDB_S'] >= 1) & (df['Age_MDB_S'] <= 25), 1, 0)
要在一行中做到这一点,有很多方法可以做到这一点
关于python - Pandas 将年龄变量分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43325040/
我正在从事一个项目,该项目要求我实现一种方法来确定工作日后一天的人的年龄;也就是每年九月的第一个星期一。 这个方法在以后的年份会用到,所以需要判断当前年份和劳动节后一天的人的年龄。 下面是我用来获取学
我已经在 Play 商店中部署了一个应用程序。我需要获取下载该应用程序的人的年龄 和性别。是否可以获取这些属性,或者我是否需要实现 Google Analytics 才能获取这些属性? 最佳答案 恐怕
我正在尝试根据出生日期计算年龄,我使用 this thread 成功地做到了这一点.但是,我的一些 DateOfBirth 具有空值,并且使用我下面的公式,结果返回为“2012”而不是(空白/空)。
我正在尝试根据财政年度结束时获取年龄。如果我的出生日期是 1962 年 1 月 16 日,那么我当前的年龄是 50 岁(假设今天的日期是 2012 年 1 月 16 日)。我可以使用什么 JavaSc
创建具有以下规范的函数: 函数名称:employee_matcher 目的:将员工姓名与提供的公司、年龄和性别相匹配 参数:公司(字符串)、年龄(整数)、性别(字符串) 返回:员工的名字和姓氏,如下所
1、根据身份证号码计算出生日期、年龄、性别(18位) 复制代码 代码如下: //获取输入身份证号码 var UUserCard = $("#UUserCard").val
适用于OA项目中这些对档案的管理 1、根据身份证号码计算出生日期、年龄、性别(18位) 复制代码代码如下: //获取输入身份证号码 var UUserCa
我将导入表中的数据INSERT INTO插入到我创建的新表中,该表包含有关人员、姓名、地址、出生日期、年龄、性别等的一般信息。 当我插入新表时,导入数据没有年龄,因此我在INSERT INTO之后执行
我如何使用 go standard 构建它? sql包让其他人可以阅读吗?我需要添加很多字段。 result, err := db.Exec( "INSERT INTO MyTable (na
是否有任何简单的 Android/Java 库/框架可以检测图片上的人脸并给我一些关于图片上的人的信息?我的意思是诸如性别、年龄、情绪(微笑、悲伤、愤怒)之类的信息...... 我试过Face++和微
我正在为我的 Android 应用程序使用 Firebase 身份验证。用户可以通过多个提供商(Google、Facebook、Twitter)登录。 成功登录后,有没有办法使用 Firebase a
我想知道是否可以用 PHP 以某种方式检查 session 时限。我浏览了 stackoverflow,但没有找到任何东西。提前感谢您的所有回答。 如果有人想知道为什么我需要知道这样的事情,我想在所有
我正在尝试使用 CakePHP 按出生日期分组并根据结果进行计数。这是我的查询。 $data = $this->User->find('all', array( 'fields' => arr
我正在尝试找出一种使用 Python 3.6 和 Boto 3 通过 aws lambda 函数让用户访问 key 年龄的方法。我的问题是我似乎无法找到正确的 api 调用(如果存在的话)以此目的。我
使用 Seaside 应用程序启动我的图像时,我在启动方法中有此代码: WAMySeasideApp>>startUp: resuming WAMySeasideApp waAppli
谷歌从哪里获得这些数据,是否有可能在没有它的情况下在我自己的网站上手动获得这些数据(假设它以某种Cookie /用户代理字符串/标识符/等形式存在)? 最佳答案 Google从第3方双击Cookie获
我在 Google App Engine 上部署了一个 Django 应用程序,该应用程序过早地将其用户注销。但是没有关闭浏览器窗口/选项卡。在 settings.py 中,我有以下代码: SESSI
我正在通过 Python 使用 YouTube API。我已经可以收集特定视频的所有评论,包括作者姓名、日期和评论内容。 我还可以使用单独的代码提取特定作者的个人信息(年龄、性别、兴趣……)。但我不能
print("how old are you") age = input(">") if age >= 24: print("you are getting old") print (age) els
我是一名优秀的程序员,十分优秀!