根据其他列的值将新列添加到数据框

时间:2019-01-17 10:48:12

标签: python pandas dataframe

我已经阅读了许多与此有关的问题,但是我无法弄清楚如何将这些解决方案应用于自己的问题。我认为我必须使用df.applydf.assign,但即使阅读文档后我也不知道如何使用

这是我的数据

d = {'switchopen': [True, True, False, False, False, False, False, False, False, False, False, False, False, True, True, False, False, True ],
     'switchclose': [ False, False, False, False, False, True, True, True, True, True, False, False, False, False, False, False, False, False]}

df = pd.DataFrame(data=d)

switchopen == True & switchclose == False

打开

switchopen == False & switchclose == True

关闭

switchopen == False & switchclose == False

时它在移动

我当前的解决方案是:

dfopen = df[(df.switchclose == False) & (df.switchopen == True)]
dfopen['Open'] = 'open'
dfclose = df[(df.switchclose == True) & (df.switchopen == False)]
dfclose['Moving'] = 'close'
dfmoving = df[(df.switchclose == False) & (df.switchopen == False)]
dfmoving['Close'] = 'moving'

dfs = [dfopen, dfclose, dfmoving]
dfz = pd.concat(dfs, sort= False)
dfz = dfz.sort_index(ascending=True)

dfz['Position'] = dfz['Position'].fillna(dfz['Open'])
dfz['Position'] = dfz['Position'].fillna(dfz['Close'])
print(dfz['Position'])

哪一个给我我想要的东西,但是我正在寻找更好的解决方案,例如:

df['Close'] = (df.switchclose == True & df.switchopen == False)
df['Open'] = (df.switchclose == False & df.switchopen == False)
df['Moving'] = (df.switchclose == False & df.switchopen == False)

然后使用.fillna

如果有解决方案甚至不需要该步骤,那将是最好的选择

预期输出是这样:

       Position
0       open
1       open
2     moving
3     moving
4     moving
5      close
6      close
7      close
8      close
9      close
10    moving
11    moving
12    moving
13      open
14      open
15    moving
16    moving
17      open

3 个答案:

答案 0 :(得分:1)

考虑到您的逻辑要求和输出期望,这应该可行

df.loc[(df['switchopen'] == True) & (df['switchclose'] == False), 'Position'] = 'open'
df.loc[(df['switchopen'] == False) & (df['switchclose'] == True), 'Position'] = 'closed'
df.loc[(df['switchopen'] == False) & (df['switchclose'] == False), 'Position'] = 'moving'

答案 1 :(得分:1)

对于多种情况,您可以使用np.select。您可以先定义条件:

c1 = df.switchopen & ~df.switchclose
c2 = ~df.switchopen & df.switchclose
c3 = ~df.switchopen & ~df.switchclose

在正确的术语上,指定从中获取输出元素的choicelist

df['position'] = np.select([c1,c2, c3], ['open','closed', 'moving'])

     switchopen  switchclose position
0         True        False     open
1         True        False     open
2        False        False   moving
3        False        False   moving
4        False        False   moving
5        False         True   closed
6        False         True   closed
7        False         True   closed
8        False         True   closed
9        False         True   closed
10       False        False   moving
11       False        False   moving
12       False        False   moving
13        True        False     open
14        True        False     open
15       False        False   moving
16       False        False   moving
17        True        False     open

答案 2 :(得分:0)

我喜欢np.where。这是解决方案:

import numpy as np
df.loc[:,'position1'] = np.where((df['switchopen'] == True) & (df['switchclose'] == False), 'open',
                        np.where((df['switchopen'] == False) & (df['switchclose'] == True), 'closed',
                        np.where((df['switchopen'] == False) & (df['switchclose'] == False), 'moving',
                                np.nan)))

如您所见,您可以为两个条件都设置NaN。