多列上的类别条件

发布于 2025-02-03 19:49:05 字数 664 浏览 2 评论 0 原文

我有一个数据集如下:

ID  A1      A2
0   A123     1234
1   1234     5568
2   5568     NaN
3   Zabc     NaN
4   3456     3456
5   3456     3456
6   NaN    NaN
7   NaN    NaN

意图是通过每列(A1和A2),确定两个列在何处空白,如第6和第7行中的位置,创建一个新列,并将其分为“ A1和A2都是空白”

我使用了以下代码:

df['Z_Tax No Not Mapped'] = np.NaN

df['Z_Tax No Not Mapped'] = np.where((df['A1'] == np.NaN) & (df['A2'] == np.NaN), 1, 0)

但是,输出将所有行捕获为新列下的所有行,'z_tax no no dobapped',但是数据具有两个列为空白的实例。不确定我在哪里犯错以过滤此类情况。

注意:A1和A2列有时是字母数字或数字。

想法是将类别放在单独的列中,因为“ ID未更新”或“ ID已更新”,以便通过将简单的过滤器放在“ ID未更新”上,我们可以识别这两个列中空白的案例。

I have a data set as below:

ID  A1      A2
0   A123     1234
1   1234     5568
2   5568     NaN
3   Zabc     NaN
4   3456     3456
5   3456     3456
6   NaN    NaN
7   NaN    NaN

Intention is to go through each column (A1 and A2), identify where both the columns are blank as in row 6 and 7, create a new column and categorise as "Both A1 and A2 are blank"

I used the below code:

df['Z_Tax No Not Mapped'] = np.NaN

df['Z_Tax No Not Mapped'] = np.where((df['A1'] == np.NaN) & (df['A2'] == np.NaN), 1, 0)

However the output captures all the rows as 0 under new column 'Z_Tax No Not Mapped', but the data have instances where both the columns are blank. Not sure where i'm making a mistake to filter such cases.

Note: Columns A1 and A2 are sometimes alphanumeric or just numeric.

Idea is to place a category in a separate column as "IDs are not updated" or "IDs are updated", so that by placing a simple filter on "IDs are not updated" we can identify cases that are blank in both columns.

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

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

发布评论

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

评论(2

溺ぐ爱和你が 2025-02-10 19:49:05

使用 > with 用于测试如果所有列均为 true s-缺失值:

df['Z_Tax No Not Mapped'] = np.where(df[['A1','A2']].isna().all(axis=1),
                                     'Both A1 and A2 are blank', 
                                      '')

Use DataFrame.isna with DataFrame.all for test if all columns are Trues - missing values:

df['Z_Tax No Not Mapped'] = np.where(df[['A1','A2']].isna().all(axis=1),
                                     'Both A1 and A2 are blank', 
                                      '')
老娘不死你永远是小三 2025-02-10 19:49:05
df.loc[df.isna().all(axis=1), "Z_Tax No Not Mapped"] = "Both A1 and A2 are blank"
df.loc[df.isna().all(axis=1), "Z_Tax No Not Mapped"] = "Both A1 and A2 are blank"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文