如何根据空值将一行列值赋给另一行列

2024-09-28 22:24:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧


    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**

Tags: matchnancol2col3col1distinctcol4col5
3条回答

根据您的逻辑,似乎您希望将Col4中的0,3值映射到Col5中的“Distinct”,将1,2值映射到“Match”。您只想更新NaN中的Col5值。你知道吗

尝试:

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)第4列中每个数字只有两个条目

2)Col4中具有相同编号的两个条目相邻放置(实际上这并不重要,如果不是这样,您可以始终按Col4对数据帧进行排序,您将有这个case)

代码如下:

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

我在这里做三步。你知道吗

1)用字符串替换所有nan,这样在使用if时就不会出现任何数据类型比较问题。你知道吗

2)按升序循环。如果第5列中的值为“None”,则替换为“txt”中的值。否则,“txt”变量将与Col5中的值一起存储。你知道吗

3)同一回路的顺序相反。你知道吗

我希望这能解决你的问题。你知道吗

我想你要的是功能np.哪里. 我假设您希望在Col4 = 0时将值'Distinct'赋值给Col5,在Col4 = 1时将值'Match'。那么您的代码将是:

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

当然,您可以根据您需要的任何条件语句修改代码

相关问题 更多 >