pandas映射并连接多个列

时间:2015-06-04 19:15:38

标签: python pandas

我需要从我的pandas df传递5列,并将得到的5个值连接到同一行。

df.head()

           BP    Pain   Pulse    RR   Temp
0     111/111       9      75    13   97.1
1        None    None    None    18     98
2      164/87       6      79    15   None
3      120/72       5      77    18     97

另一个名为VSCleaner

的模块
VSCleaner.CleanVSSET(df.BP, df.Pain, df.Pulse, df.RR, df.Temp)

将返回类似

的内容
{'BP': '120/40', 'Pain': 2, 'Pulse': 180, 'RR': 18, 'Temp': 66.0}, True

我需要将每个df行传递给该函数,并将生成的dict和bool中的值插入同一行(cleaned_BP, cleaned_Pain, cleaned_Pulse, cleaned_RR, cleaned_Temp , BOOL isCleaned)并在我的所有df中运行。

2 个答案:

答案 0 :(得分:1)

我找到的最简单方法:

>>> def CleanVSSET_apply(x):
        # in my case CleanVSSET just returns the same values
        d = CleanVSSET(*x.values)
        r = d[0]
        r['Cleaned'] = d[1]
        return pd.Series(r)
>>>
>>> df_cleaned = df.apply(CleanVSSET_apply, axis=1)
        BP Cleaned  Pain Pulse  RR  Temp
0  111/111    True     9    75  13  97.1
1     None    True  None  None  18    98
2   164/87    True     6    79  15  None
3   120/72    True     5    77  18    97

>>> pd.merge(df, df_cleaned, left_index=True, right_index=True, suffixes=['', 'cleaned'])
        BP  Pain Pulse  RR  Temp BP_cleaned Cleaned Pain_cleaned  \
0  111/111     9    75  13  97.1    111/111    True            9   
1     None  None  None  18    98       None    True         None   
2   164/87     6    79  15  None     164/87    True            6   
3   120/72     5    77  18    97     120/72    True            5   

  Pulse_cleaned  RR_cleaned Temp_cleaned  
0            75          13         97.1  
1          None          18           98  
2            79          15         None  
3            77          18           97  

答案 1 :(得分:0)

这就是我最后所做的工作并返回所需的输出。

for index, row in df.iterrows():
    cleaned_dict, isSwapped = vscleaner.CleanVSSet(row['Pulse'], row['RR'], row['Pain'], row['Temp'], row['BP'])
    df.loc[index, 'Pulse_cleaned'] = cleaned_dict['Pulse']
    df.loc[index, 'RR_cleaned'] = cleaned_dict['RR']
    df.loc[index, 'Pain_cleaned'] = cleaned_dict['Pain']
    df.loc[index, 'Temp_cleaned'] = cleaned_dict['Temp']
    df.loc[index, 'BP_cleaned'] = cleaned_dict['BP']
    df.loc[index, 'isSwapped'] = isSwapped