Python 按值将一列拆分为两列
我有一个给定的列,其中我有5个不同的值。我需要创建新列,如下所示: A和B都是-->col1 A和B都不去-->col1 A去B不去-->col2 A不去B去-->col2 不知道-->如果值为NaN,则两者都是Python 按值将一列拆分为两列,python,pandas,Python,Pandas,我有一个给定的列,其中我有5个不同的值。我需要创建新列,如下所示: A和B都是-->col1 A和B都不去-->col1 A去B不去-->col2 A不去B去-->col2 不知道-->如果值为NaN,则两者都是 Given_Col Expexted_Col1 Expexted_Col2 Both A and B goes Both A and B goes
Given_Col Expexted_Col1 Expexted_Col2
Both A and B goes Both A and B goes No idea
Neither A nor B goes Neither A nor B goes No idea
A goes B doesn't go No idea A goes B doesn't go
A doesn't go B goes No idea A doesn't go B goes
A goes B doesn't go No idea A goes B doesn't go
Neither A nor B goes Neither A nor B goes No idea
No idea No idea No idea
Both A and B goes Both A and B goes No idea
我想不出任何解决办法。实际的方法是什么
注意:我考虑过复制现有列并映射值,也许?我认为两个条件列分配应该可以工作 每一个都会根据选择条件为列拾取有效条目。如果您有五种以上的可能性,这可能是不合适的,但对于这种情况,它应该足够有效
df['Expexted_Col1'] = df.apply(lambda x: x['Given_Col'] if (x['Given_Col'] == 'Both A and B goes' or x['Given_Col'] == 'Neither A nor B goes') else 'No idea', axis = 1)
df['Expexted_Col2'] = df.apply(lambda x: x['Given_Col'] if (x['Given_Col'] == "A goes B doesn't go" or x['Given_Col'] == "A doesn't go B goes") else 'No idea', axis = 1)
我认为两个有条件的列分配应该有效 每一个都会根据选择条件为列拾取有效条目。如果您有五种以上的可能性,这可能是不合适的,但对于这种情况,它应该足够有效
df['Expexted_Col1'] = df.apply(lambda x: x['Given_Col'] if (x['Given_Col'] == 'Both A and B goes' or x['Given_Col'] == 'Neither A nor B goes') else 'No idea', axis = 1)
df['Expexted_Col2'] = df.apply(lambda x: x['Given_Col'] if (x['Given_Col'] == "A goes B doesn't go" or x['Given_Col'] == "A doesn't go B goes") else 'No idea', axis = 1)
您可以使用几个np来实现这一点。其中函数:
df['col1'] = np.where(df['Given_Col'] == 'Both A and B goes', 'Both A and B goes', df['col1'])
df['col2'] = np.where(df['Given_Col'] == 'Both A and B goes', 'No idea', df['col1'])
df['col1'] = np.where(df['Given_Col'] == 'Neither A nor B goes', 'Neither A nor B goes', df['col2'])
df['col2'] = np.where(df['Given_Col'] == 'Neither A nor B goes', 'No idea', df['col2'])
您可以从这里继续……您可以使用几个np函数来完成此操作。其中函数:
df['col1'] = np.where(df['Given_Col'] == 'Both A and B goes', 'Both A and B goes', df['col1'])
df['col2'] = np.where(df['Given_Col'] == 'Both A and B goes', 'No idea', df['col1'])
df['col1'] = np.where(df['Given_Col'] == 'Neither A nor B goes', 'Neither A nor B goes', df['col2'])
df['col2'] = np.where(df['Given_Col'] == 'Neither A nor B goes', 'No idea', df['col2'])
您可以从这里继续…单向使用
pandas.DataFrame.assign
withfillna
:
mapper = {'col1': ['Both A and B goes', 'Neither A nor B goes'],
'col2': ["A goes B doesn't go", "A doesn't go B goes"]}
s = df["Given_Col"]
new_df = df.assign(**{k: s[s.isin(v)] for k, v in mapper.items()}).fillna("No idea")
print(new_df)
输出:
Given_Col col1 col2
0 Both A and B goes Both A and B goes No idea
1 Neither A nor B goes Neither A nor B goes No idea
2 A goes B doesn't go No idea A goes B doesn't go
3 A doesn't go B goes No idea A doesn't go B goes
4 A goes B doesn't go No idea A goes B doesn't go
5 Neither A nor B goes Neither A nor B goes No idea
6 No idea No idea No idea
7 Both A and B goes Both A and B goes No idea
单向使用
pandas.DataFrame.assign
和fillna
:
mapper = {'col1': ['Both A and B goes', 'Neither A nor B goes'],
'col2': ["A goes B doesn't go", "A doesn't go B goes"]}
s = df["Given_Col"]
new_df = df.assign(**{k: s[s.isin(v)] for k, v in mapper.items()}).fillna("No idea")
print(new_df)
输出:
Given_Col col1 col2
0 Both A and B goes Both A and B goes No idea
1 Neither A nor B goes Neither A nor B goes No idea
2 A goes B doesn't go No idea A goes B doesn't go
3 A doesn't go B goes No idea A doesn't go B goes
4 A goes B doesn't go No idea A goes B doesn't go
5 Neither A nor B goes Neither A nor B goes No idea
6 No idea No idea No idea
7 Both A and B goes Both A and B goes No idea
映射器是从哪里来的?再进一步,我可以用你一次做的方式,将A和B都替换为1,A和B都不替换为-1,
不知道
都替换为0吗?然后你可以制作一个新的映射器
(类似于{col1:{“A和B都去了”:1,
),并乘以布尔数组。顺便说一句,在同一篇文章中问另一个问题从来都不是一个好主意,尤其是通过注释,因为不仅注释部分在提供正确答案方面有限制,而且也不能保证回答者知道下一部分的答案;)。映射器来自哪里?再进一步,我可以替换A和B都变成1,A和B都变成-1,不知道
变成0,就像你现在做的那样?然后你会制作一个新的映射器(类似于{“col1”:{“A和B都变成”:1,
),并乘以布尔数组。顺便说一句,在同一篇文章中问另一个问题从来都不是一个好主意,尤其是通过评论,因为不仅评论部分在提供正确答案方面有限制,而且也无法保证回答者知道下一部分的答案;)。