根据条件从母数据框创建子熊猫数据框

时间:2020-10-12 13:55:14

标签: python pandas dataframe

我有一个如下数据框。实际上,此数据帧是通过两个表的outer连接来完成的。

IndentID    IndentNo    role_name     role_id    user_id       ishod    isdesignatedhod   Flag
100         1234         xyz            3         17            1         nan           right_only
nan         nan          nan           -1         -1            None      None          right_only
nan         nan          nan            1         15            None      None          right_only
nan         nan          nan           100        100           None       1            right_only

目标:我想要一个基于列条件的结果数据框。条件在下面给出

if ishod == 1 the resultant df will be:
IndentID    IndentNo    role_name     role_id    user_id
100         1234         xyz            3         17

if ishod!=1 and isdesignatedhod==1 the resultant df will be:
IndentID    IndentNo    role_name     role_id    user_id
100         1234         xyz            100         100

我真的不知道该如何进行。任何线索将不胜感激!

3 个答案:

答案 0 :(得分:0)

这应该可以满足您的要求

nan = float("nan")
def func(row):
    if row["ishod"] == "1":
        return pd.Series([100, 1234, "xyz", 3, 17, nan, nan, nan], index=row.index)
    elif row["isdesignatedhod"] == "1":
        return pd.Series([100, 1234, "xyz", 100, 100, nan, nan, nan], index=row.index)
    else:
        return row


pd.read_csv(io.StringIO(
"""IndentID    IndentNo    role_name     role_id    user_id       ishod    isdesignatedhod   Flag
100         1234         xyz            3         17            1         nan           right_only
nan         nan          nan           -1         -1            None      None          right_only
nan         nan          nan            1         15            None      None          right_only
nan         nan          nan           100        100           None       1            right_only
"""), sep=" +", engine='python')\
.apply(func,axis=1)

输出:

   IndentID  IndentNo role_name  role_id  user_id ishod isdesignatedhod        Flag
0     100.0    1234.0       xyz        3       17   NaN             NaN         NaN
1       NaN       NaN       NaN       -1       -1  None            None  right_only
2       NaN       NaN       NaN        1       15  None            None  right_only
3     100.0    1234.0       xyz      100      100   NaN             NaN         NaN

答案 1 :(得分:0)

要基于特定列中的值选择行,可以使用以下表示法:

df[ df["column_name"] == value_to_keep ]

以下是此操作的示例:

import pandas as pd

d = {'col1': [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1],
     'col2': [3,4,5,3,4,5,3,4,5,3,4,5,3,4,5],
     'col3': [6,7,8,9,6,7,8,9,6,7,8,9,6,7,8]}
# create a dataframe 
df = pd.DataFrame(d)

这是df的样子:

In [17]: df
Out[17]: 
    col1  col2  col3
0      1     3     6
1      2     4     7
2      1     5     8
3      2     3     9
4      1     4     6
5      2     5     7
6      1     3     8
7      2     4     9
8      1     5     6
9      2     3     7
10     1     4     8
11     2     5     9
12     1     3     6
13     2     4     7
14     1     5     8

现在选择第一列中值为“ 2”的所有行:

df_1 = df[df["col1"] == 2]

In  [19]: df_1
Out [19]: 
    col1  col2  col3
1      2     4     7
3      2     3     9
5      2     5     7
7      2     4     9
9      2     3     7
11     2     5     9
13     2     4     7

您还可以通过这种方式选择多种条件:

df_2 = df[(df["col2"] >= 4) & (df["col3"] != 7)]


In  [22]: df_2
Out [22]: 
    col1  col2  col3
2      1     5     8
4      1     4     6
7      2     4     9
8      1     5     6
10     1     4     8
11     2     5     9
14     1     5     8

希望这个示例有所帮助!

答案 2 :(得分:0)

安德烈给出了正确的答案。另外,您还必须牢记列ishodisdesignatedhod的dtype。它们是“对象”类型,在这种情况下特别是“字符串”。 因此,在将这些对象列与数字进行比较时,必须使用“引号”。

df[df["ishod"] == "1"]