簡體   English   中英

按值將pandas中的一列拆分為兩列

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

我有一個Given_Col ,其中有 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               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)

使用的方法之一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)

輸出:

              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

你可以用幾個 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'])

你可以從這里繼續......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM