将nan值从一个数据帧复制到另一个数据帧

时间:2017-04-25 03:07:41

标签: python pandas mask

我有两个相同大小和尺寸的数据框import numpy as np import pandas as pd import scipy.stats as stats import matplotlib.pyplot as plt EI_levels = ["very low","low","medium","high","very high"] #generate dataframe of values EI = pd.DataFrame({'level': np.random.choice(EI_levels,p = [0.05, 0.05, 0.2, 0.4, 0.3],size=500)}) #generate contingency table of counts EI_df = pd.DataFrame(EI.level.value_counts().reset_index()) EI_df.columns = ['level', 'count'] EI_df.set_index('level') #Generate bar chart ax = EI_df[['level', 'count']].plot(kind='bar', title ="Emotional Intelligence",figsize=(15, 10)) ax.xticks = (EI_df.level, ["very low","low","medium","high","very high"]) plt.show() df1。是否有一种简单的方法可以复制' df1'中的所有df2值到' df2' ?下面的示例演示了我想要的输出 NaN

.copynans()

2 个答案:

答案 0 :(得分:3)

无论

df1.where(df2.notnull())

或者

df1.mask(df2.isnull())

答案 1 :(得分:2)

#Use null cells from df1 as index to set the the corresponding cell to nan in df2
df2[df1.isnull()]=np.nan