gpt4 book ai didi

python - Pandas 过滤并创建新列

转载 作者:行者123 更新时间:2023-12-04 00:12:55 26 4
gpt4 key购买 nike

我有一个 Pandas df:

import pandas as pd
import numpy as np
df = pd.DataFrame(['Air type:1', 'Space kind:2', 'water', np.NaN], columns = ['A'])

A
0 Air type:1
1 Space kind:2
2 water
3 NaN

我想将 A 中包含“:”的条目拆分为两个新列。因此,我尝试将此操作与 .loc 过滤器结合使用:

df.loc[(df.A.str.contains(':')) & (~df.A.isnull()), ['B', 'C']] = df.A.str.split(':', expand = True)

虽然结果不太乐观:

     A            B       C
0 Air type:1 NaN NaN
1 Space kind:2 NaN NaN
2 water NaN NaN
3 NaN NaN NaN

如果我不过滤它会起作用:

df[['B', 'C']] = df.A.str.split(':', expand = True)

A B C
0 Air type:1 Air type 1
1 Space kind:2 Space kind 2
2 water water None
3 NaN NaN NaN

问题是 water 条目被错误地分配给了新列,之后我必须手动修复它。

为什么 .loc + assign 不起作用?

理想情况下我想得到:

           A           B        C
0 Air type:1 Air type 1
1 Space kind:2 Space kind 2
2 water NaN NaN
3 NaN NaN NaN

最佳答案

尝试使用 df.where 进行检查的条件:

c  = c = df['A'].str.contains(":")
#c = df['A'].str.count(":").ge(1)
df[['B', 'C']] = df['A'].str.split(":",expand=True).where(c)

print(df)
A B C
0 Air type:1 Air type 1
1 Space kind:2 Space kind 2
2 water NaN NaN
3 NaN NaN NaN

关于python - Pandas 过滤并创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67036638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com