How can I erase empty values in each column but not erasing other column's value?

Viewed 50
Column A Column B Column C Column D
200 230 200 230
250 NaN 250 NaN
NaN 350 NaN 350

How can I erase only NaN value but not erasing other columns' existing value in the same row? For instance if I use dropna() command on column A to drop the NaN value, it will erase the column B and Column D values (350) in the same row.

After erasing NaN value only, I want to plot it as box plot Now I got image with two boxplots for every columns since I couldn't erase the NaN value..

Two boxplots for one column

I tried to remove nan value using a command like

df_BZ_CO2.dropna(subset = ['CDL-08','CDL-09', 'CDL-10', 'CDL-11'], inplace = True)

however, they dropped all rows with values..

-- I'm adding the code that I used

df = pd.read_excel('Datablock-CO2.xlsx', sheet_name=0, keep_default_na=False)  

for i in range(0, len(df.columns)):
    df.iloc[:,i] = pd.to_numeric(df.iloc[:,i], errors='ignore')
    # errors='ignore' lets strings remain as 'non-null objects'
    
    
df = df.astype({ 'Time' : 'string','Office_hours': 'string', 'Ventilation': 'string', 'Ventilation_type':'string', 
                'Presence_Subject1': 'string', 'Week':'string', 'Building': 'string'})

df_BZ_CO2 = df.iloc[:, 35:39] #BZ: HOBO BZ (4)
df_IE_CO2 = df.iloc[:, 40]  #IE: Inhalation Exposures of Subject 01
df_BZ_CO2.fillna(0, inplace=True)
df_IE_CO2.fillna(0, inplace=True)

fig, axs = plt.subplots(2,1,figsize=(5,5)) 
# the size of the subplots within the figure individually by means of gridspec_kw

a= sns.boxplot(data = df_BZ_CO2, ax=axes[0], color ='#698B69')
b= sns.boxplot(data = df_IE_CO2, ax=axes[1], color ='#C1FFC1')

a.set_title("Personal Clouds", fontsize=18)
b.set_title("Inhalation exposures", fontsize=18)

# Spacing
plt.subplots_adjust(left= 0.11, bottom=0.25, right=0.9, top=0.85, wspace=0.12, hspace=0.3)

# fig.text(0.5, 0.04, 'Stationary monitors', ha='center', va='center', fontsize = 16)
fig.text(0.06, 0.5, 'CO2 concentration (ppm)', ha='center', va='center', rotation='vertical', fontsize = 16)

# threshold
a.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
a.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

b.axhline(y=1000, color='red', linestyle='--', linewidth = 1.5)
b.axhline(y=800, color='#8B2323', linestyle='--', linewidth = 1.5)

#common title
fig.suptitle('Breathing zone CO2', fontsize = 18)

min([], default="EMPTY")
# returns EMPTY

# Defining custom 'xlim' and 'ylim' values.
custom_ylim = (300, 1800)

# Setting the values for all axes.
plt.setp(axes, ylim=custom_ylim, yticks=[300,600, 900 ,1200, 1500])
axes[0].set_yticklabels([300,600, 900 ,1200, 1500], fontsize=14)
axes[1].set_yticklabels([300,600, 900 ,1200, 1500], fontsize=14)

# xticks rotate
for ax in axes.flatten():
    plt.sca(ax)
    plt.xticks(rotation = 45)
    
plt.rcParams['xtick.labelsize'] = 14
    
plt.show()
0 Answers
Related