简体   繁体   English

按值将pandas中的一列拆分为两列

[英]Split one column into two columns in pandas by the values

I have a Given_Col in which I have 5 different values.我有一个Given_Col ,其中有 5 个不同的值。 I need to create new columns as follows:我需要按如下方式创建新列:

Both A and B goes ---> col1 A 和 B 都去 ---> col1

Neither A nor B goes ---> col1 A 和 B 都不行 ---> col1

A goes B doesn't go ---> col2 A 去 B 不去 ---> col2

A doesn't go B goes ---> col2 A 不去 B 去 ---> col2

No idea ---> both if there the value is NaN不知道 ---> 如果值都是 NaN

      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 

I couldn't think of any solution.我想不出任何解决办法。 What would be the practical way?什么是实用的方法?

Note : I considered duplicating existing column and mapping the values maybe?注意:我考虑过复制现有列并映射值吗?

I think two conditional column assignments should work.我认为两个条件列分配应该有效。

Each one picks up valid entries for the column based on selection criteria.每个人都根据选择标准为该列选择有效的条目。 If you had more than five possibilities, this could be unweildy, but it should work well enough for this case.如果您有五个以上的可能性,这可能会很笨拙,但对于这种情况它应该足够好。

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)

One way using pandas.DataFrame.assign with fillna :使用的方法之一pandas.DataFrame.assignfillna

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)

Output:输出:

              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

You can do that with few np.where functions:你可以用几个 np.where 函数来做到这一点:

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'])

You can continue from here....你可以从这里继续......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM