简体   繁体   English

通过在pandas中查找另一个数据框来填充数据框

[英]Populating a dataframe by looking up another dataframe in pandas

I have a pandas dataframe (df) like below: 我有一个像下面这样的pandas数据帧(df):

AccountName   AccountName2  DateTime
abc           guest         2016-06-10 20:46
              guest         2016-06-10 21:32
def                         2016-06-10 23:11
                            2016-06-10 23:31
ghi                         2016-06-10 24:41

I need to derive a new dataframe (df1) based on the above dataframe. 我需要根据上面的数据框导出一个新的数据帧(df1)。 df1 should have 2 fields, ResultAccount and DateTime. df1应该有2个字段,ResultAccount和DateTime。

if(df["AccountName"] != ' '):
 df1["ResultAccount"] = df["AccountName"]
elif(df["AccountName2] != ' '):
 df1["ResultAccount"] = df["AccountName2"]
else:
 df1["ResultAccount"] = "none"

This is the approach I followed but df1 is not getting populated as expected. 这是我遵循的方法,但df1没有按预期填充。 Any help would be appreciated. 任何帮助,将不胜感激。

I think you can first replace strings with spaces ' ' to NaN and then apply custom function f with last_valid_index . 我想你可以先用空格' ' replace NaN字符串,然后用last_valid_index apply自定义函数f Output is Dataframe from Series ResultAccount and df.DateTime : 输出DataframeSeries ResultAccountdf.DateTime

import pandas as pd
import numpy as np

df = pd.DataFrame({'AccountName2': {0: 'guest', 1: 'guest', 2: ' ', 3: ' ', 4: ' '}, 
                   'DateTime': {0: '2016-06-10 20:46', 1: '2016-06-10 21:32', 2: '2016-06-10 23:11', 3: '2016-06-10 23:31', 4: '2016-06-10 24:41'}, 
                   'AccountName': {0: 'abc', 1: ' ', 2: 'def', 3: ' ', 4: 'ghi'}})

print (df)
  AccountName AccountName2          DateTime
0         abc        guest  2016-06-10 20:46
1                    guest  2016-06-10 21:32
2         def               2016-06-10 23:11
3                           2016-06-10 23:31
4         ghi               2016-06-10 24:41
df[['AccountName','AccountName2']] = df[['AccountName','AccountName2']].replace(' ',np.nan)

def f(x):
    if x.first_valid_index() is None:
        return 'None'
    else:
        return x[x.first_valid_index()]

ResultAccount = (df[['AccountName','AccountName2']].apply(f, axis=1))

df1 = pd.DataFrame({'ResultAccount':ResultAccount ,'DateTime':df.DateTime}, 
                   columns=['ResultAccount','DateTime'])

print (df1)
  ResultAccount          DateTime
0           abc  2016-06-10 20:46
1         guest  2016-06-10 21:32
2           def  2016-06-10 23:11
3          None  2016-06-10 23:31
4           ghi  2016-06-10 24:41

You could use np.select . 你可以使用np.select It is a multi-conditional generalization of np.where : 它是np.where的多条件泛化:

import numpy as np
import pandas as pd
df = pd.DataFrame(
    {'AccountName': ['abc', ' ', 'def', ' ', 'ghi'],
     'AccountName2': ['guest', 'guest', ' ', ' ', ' '],
     'DateTime': ['2016-06-10 20:46', '2016-06-10 21:32', '2016-06-10 23:11', '2016-06-10 23:31', '2016-06-10 24:41']})

conditions = [df['AccountName'] != ' ', df['AccountName2'] != ' ']
choices = [df["AccountName"], df["AccountName2"]]
df['ResultAccount'] = np.select(conditions, choices, default='none')

yields 产量

  AccountName AccountName2          DateTime ResultAccount
0         abc        guest  2016-06-10 20:46           abc
1                    guest  2016-06-10 21:32         guest
2         def               2016-06-10 23:11           def
3                           2016-06-10 23:31          none
4         ghi               2016-06-10 24:41           ghi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM