pandas中当某列满足制定条件时,对另一列赋值的问题

发布于 2022-09-11 20:57:58 字数 446 浏览 10 评论 0

各位大神,想请教一个简单的问题,上网各种查都查不到,我现在想实现df数据中当点评状态列不为null的时候,是否有评价列的值都为1。完全摸不到头脑,这个应该怎么写呢?一点头绪都没有,希望大家帮忙,谢谢

clipboard.png
我尝试用了一下这个代码,结果那是相当的不对了,应该怎么办呢,新手基础非常薄弱,恳请大家指教

import pandas as pd
import numpy as np
df = pd.read_excel('D:/谷歌下载/明细.xlsx', sheet_name='sheet1')
df['是否有房评']=df['是否有房评'].apply(str)
df.loc[df['点评状态']!='null', '是否有房评'] = "1"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸伞微斜 2022-09-18 20:57:58

用列表解析式进行判断:

df["has_comment_or_not"] = ["1" if status != "null" else "0" 
                            for status in df["comment_status"].apply(str)]

具体例子如下:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: df = pd.DataFrame(
    {"has_comment_or_not":['0','0','0','1','0','0'],
    "comment_status":["null","null","120047007","null","null","122121"]}
    )

In [4]: df
Out[4]:
  has_comment_or_not comment_status
0                  0           null
1                  0           null
2                  0      120047007
3                  1           null
4                  0           null
5                  0         122121


In [5]: df["has_comment_or_not"] = ["1" if status != "null" else "0" 
for status in df["comment_status"].apply(str)]

In [6]: df
Out[6]:
  has_comment_or_not comment_status
0                  0           null
1                  0           null
2                  1      120047007
3                  0           null
4                  0           null
5                  1         122121

如果只想修改comment_status有内容,改has_comment_or_not为1,只需要对处理的结果,再进行列表解析判断即可

df["has_comment_or_not"] =[item if item == '1' else df["has_comment_or_not"].values[index] for index, item in enumerate(["1" if status != "null" else "0" for status
    ...:  in df["comment_status"].apply(str)])]

例子如下:(修改了has_comment_or_not的原始值,添加了2,方便区分)

In [44]: import pandas as pd

In [45]: import numpy as np

In [46]: df = pd.DataFrame(
    ...:     {"has_comment_or_not":['1','2','0','1','0','0'],
    ...:     "comment_status":["null","null","120047007","null","null","122121"]}
    ...:     )

In [47]: ["1" if status != "null" else "0"
    ...: for status in df["comment_status"].apply(str)]
Out[47]: ['0', '0', '1', '0', '0', '1']

In [49]: [ item if item =='1' else df["has_comment_or_not"].values[index] for index, item in enumerate(['0', '0', '1', '0', '0', '1'])]
Out[49]: ['1', '2', '1', '1', '0', '1']

In [50]: df["has_comment_or_not"] =[item if item == '1' else df["has_comment_or_not"].values[index] for index, item in enumerate(["1" if status != "null" else "0" for status
    ...:  in df["comment_status"].apply(str)])]
    
In [51]: df
Out[51]:
  has_comment_or_not comment_status
0                  1           null
1                  2           null
2                  1      120047007
3                  1           null
4                  0           null
5                  1         122121

当然也可以使用iterrows方法进行判断修改:

In [58]: row_list = []
    ...: for index, row in df.iterrows():
    ...:     if row["comment_status"] !="null":
    ...:         row["has_comment_or_not"] = "1"
    ...:     row_list.append(row)

In [60]: df = pd.DataFrame(row_list)

In [61]: df
Out[61]:
  has_comment_or_not comment_status
0                  1           null
1                  2           null
2                  1      120047007
3                  1           null
4                  0           null
5                  1         122121
迷迭香的记忆 2022-09-18 20:57:58
f = lambda s: 1 if s["点评状态"]!="null" else 0

df['是否有房评'] = df.apply(f, axis=1)
陈年往事 2022-09-18 20:57:58
# coding: utf-8

import pandas as pd

data = [
    {'是否有房': 0, '点评状态': 'null'},
    {'是否有房': 0, '点评状态': '111'},
    {'是否有房': 0, '点评状态': 'null'},
]

df = pd.DataFrame(data)
df['是否有房'] = df['点评状态'].apply(lambda x: 1 if x != 'null' else 0)
print df
紫瑟鸿黎 2022-09-18 20:57:58

貌似比较简单和高效的写法应该是:

df.loc[df['点评状态'].notnull(), '是否有房评'] = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文