Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set nans across multiple pandas dataframes

I have number of similar dataframes where I would like to standardize the nans across all the dataframes. For instance, if a nan exists in df1.loc[0,'a'] then ALL other dataframes should be set to nan for the same index location.

I am aware that I could group the dataframes to create one big multiindexed dataframe but sometimes I find it easier to work with a group of dataframes of the same structure.

Here is an example:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.reshape(np.arange(12), (4,3)), columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.reshape(np.arange(12), (4,3)), columns=['a', 'b', 'c'])
df3 = pd.DataFrame(np.reshape(np.arange(12), (4,3)), columns=['a', 'b', 'c'])

df1.loc[3,'a'] = np.nan
df2.loc[1,'b'] = np.nan
df3.loc[0,'c'] = np.nan

print df1
print ' ' 
print df2
print ' ' 
print df3

Output:

     a   b   c
0  0.0   1   2
1  3.0   4   5
2  6.0   7   8
3  NaN  10  11

   a     b   c
0  0   1.0   2
1  3   NaN   5
2  6   7.0   8
3  9  10.0  11

   a   b     c
0  0   1   NaN
1  3   4   5.0
2  6   7   8.0
3  9  10  11.0

However, I would like df1, df2 and df3 to have nans in the same locations:

print df1
     a     b     c
0  0.0   1.0   NaN
1  3.0   NaN   5.0
2  6.0   7.0   8.0
3  NaN  10.0  11.0

Using the answer provided by piRSquared, I was able to extend it for dataframes of different sizes. Here is the function:

def set_nans_over_every_df(df_list):
    # Find unique index and column values
    complete_index = sorted(set([idx for df in df_list for idx in df.index]))
    complete_columns = sorted(set([idx for df in df_list for idx in df.columns]))

    # Ensure that every df has the same indexes and columns
    df_list = [df.reindex(index=complete_index, columns=complete_columns) for df in df_list]

    # Find the nans in each df and set nans in every other df at the same location     
    mask = np.isnan(np.stack([df.values for df in df_list])).any(0)
    df_list = [df.mask(mask) for df in df_list]

    return df_list

And an example using different sized dataframes:

df1 = pd.DataFrame(np.reshape(np.arange(15), (5,3)), index=[0,1,2,3,4], columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.reshape(np.arange(12), (4,3)), index=[0,1,2,3], columns=['a', 'b', 'c'])
df3 = pd.DataFrame(np.reshape(np.arange(16), (4,4)), index=[0,1,2,3], columns=['a', 'b', 'c', 'd'])

df1.loc[3,'a'] = np.nan
df2.loc[1,'b'] = np.nan
df3.loc[0,'c'] = np.nan

df1, df2, df3 = set_nans_over_every_df([df1, df2, df3])

print df1

     a     b     c   d
0  0.0   1.0   NaN NaN
1  3.0   NaN   5.0 NaN
2  6.0   7.0   8.0 NaN
3  NaN  10.0  11.0 NaN
4  NaN   NaN   NaN NaN
like image 983
KieranL Avatar asked Dec 19 '22 10:12

KieranL


1 Answers

I'd set up a mask in numpy then use this mask in the pd.DataFrame.mask method

mask = np.isnan(np.stack([d.values for d in [df1, df2, df3]])).any(0)

print(df1.mask(mask))

     a     b     c
0  0.0   1.0   NaN
1  3.0   NaN   5.0
2  6.0   7.0   8.0
3  NaN  10.0  11.0

print(df2.mask(mask))

     a     b     c
0  0.0   1.0   NaN
1  3.0   NaN   5.0
2  6.0   7.0   8.0
3  NaN  10.0  11.0

print(df3.mask(mask))

     a     b     c
0  0.0   1.0   NaN
1  3.0   NaN   5.0
2  6.0   7.0   8.0
3  NaN  10.0  11.0
like image 185
piRSquared Avatar answered Jan 04 '23 22:01

piRSquared