在熊猫中使用Or语句分配值?

时间:2019-02-26 02:10:28

标签: python pandas dataframe

我有两个数据框

df1

@Suppress("DEPRECATION")
fun Activity.setTurnScreenOnLock() {
    val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager?
    when {
        android.os.Build.VERSION.SDK_INT >= 27 -> {
            setShowWhenLocked(true)
            setTurnScreenOn(true)
            keyguardManager?.requestDismissKeyguard(this, null)
        }
        android.os.Build.VERSION.SDK_INT == 26 -> {
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
            keyguardManager?.requestDismissKeyguard(this, null)
        }
        else -> {
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
        }
    }

    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

df2

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTurnScreenOnLock(this);
        setContentView(R.layout.some_activity);
}

我正在尝试使用Or语句从df1 ['a']或df2 ['d']中分配df3 ['i']

我想要的输出:

df3

     a         b      c

0   Sussex    NaN     NaN

对于单个值,我分配了df3 ['i'] = df2 ['d']

3 个答案:

答案 0 :(得分:0)

问题尚不完全清楚。我在这里什么?如果df1 ['a']或df2 ['d']是第一次迭代,则第二次迭代是df1 ['b']或df2 ['e']吗?迭代在哪里结束?漫长的解决方案,不确定是不是您要的

lo_send()

答案 1 :(得分:0)

使用:

df3['i'] = df2['d'].item() or df1['a'].item()

除了@kerwei的答案之外,您还可以使用fillna

df3 = df1.fillna(df2)

答案 2 :(得分:0)

您的问题确实很模糊,但是我的猜测是您要叠加2个数据帧,并且输出将是一个数据帧,其中每个单元格都填充有df1或df2中的值,无论哪个都不为空。在这种情况下,两个数据帧的列索引应该匹配,例如:

import pandas as pd

df1 = pd.DataFrame({'a':['sussex'], 'b':[None], 'c':[None]})
df2 = pd.DataFrame({'a':[None], 'b':[None], 'c':[None], 'd':[None]})

>>>df1.combine_first(df2)
        a     b     c   d
0  Sussex  None  None NaN