簡體   English   中英

如何根據 null 值將一個行列值分配給另一行列

[英]How to assign one row column value to another row column based on null value

我有一個 dataframe


    Col1    Col2    Col3     Col4    Col5
    A123    13500  2/03/19    0      NaN
    B123    2000   3/04/19    0     Distinct
    C123    500    8/09/19    1      Match
    D123    100    11/01/19   1      NaN
    E123    1350  2/03/19      2         NaN
    F123    2000   3/04/19    2     Match
    G123    500    8/09/19    3      Distinct
    H123    100    11/01/19   3      NaN

我想遍歷基於Col4的行並相應地更新Col5(NaN)行。

也就是說,當我選擇Col4為 0 的行時,我想根據其他行列值更新Col5

Output:

    Col1    Col2    Col3     Col4    Col5
    A123    13500  2/03/19    0     **Distinct**
    B123    2000   3/04/19    0     Distinct
    C123    500    8/09/19    1      Match
    D123    100    11/01/19   1      **Match**
    E123    1350  2/03/19      2        **Match**
    F123    2000   3/04/19    2      Match
    G123    500    8/09/19    3      Distinct
    H123    100    11/01/19   3     **Distinct**

我認為您正在尋找的是 function np.where。 我假設您想在Col4 = 0時將值 'Distinct' 分配給Col5 ,而在Col4 = 1時將值分配給 'Match' 。 然后你的代碼將是:

df['Col5'] = np.where(df.Col4==0, 'Distinct', 'Match')

當然,您可以針對所需的任何條件語句調整代碼

根據您的邏輯,您似乎希望將 Col4 中的Col5值 0,3 設置為Col4中的“Distinct”,並將值 1,2 設置為“匹配”。 您只想更新Col5中的NaN值。

嘗試:

df = pd.DataFrame({'Col4': [0,1,2,3,0,1,2,3],
                  'Col5': ["Distinct", "Match", "Match", "Distinct", np.nan, np.nan, np.nan, np.nan]})

mapper = {
             0: "**Distinct**",
             1: "**Match**",
             2: "**Match**",
             3: "**Distinct**"
         }


df.loc[df.Col5.isna(), 'Col5'] = df[df.Col5.isna()]['Col4'].map(mapper)

你現在得到:

   Col4          Col5
0     0      Distinct
1     1         Match
2     2         Match
3     3      Distinct
4     0  **Distinct**
5     1     **Match**
6     2     **Match**
7     3  **Distinct**

如果您改變對邏輯或替換值的想法,這使得以后更改映射變得容易。

好的,我在這里假設兩件事:

1) Col 4 中的每個數字只有兩個條目

2)Col4中相同編號的兩個條目彼此相鄰放置(實際上沒關系,如果不是這種情況,您可以隨時按Col4對dataframe進行排序,您就會遇到這種情況)

代碼如下:

df = df.replace(np.nan,"None")
txt = "None"
for i in range(df.Col4.size):
    if (df.loc[i,'Col5']=="None"):
        df.loc[i,'Col5'] = txt
        txt = "None"
    else: 
        txt = df.loc[i,'Col5']

txt = "None"
for i in reversed(range(df.Col4.size)):
    if (df.loc[i,'Col5']=="None"):
        df.loc[i,'Col5'] = txt
        txt = "None"
    else: 
        txt = df.loc[i,'Col5']

我在這里做 3 個步驟。

1)用字符串替換所有的nan,這樣我在使用if時沒有任何數據類型比較問題。

2) 循環升序。 如果 Col5 中的值為“None”,則將其替換為“txt”中的值。 否則,'txt' 變量存儲 Col5 中的值。

3)相同的循環以相反的順序。

我希望這能解決你的問題。

暫無
暫無

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

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