Skip to content

Instantly share code, notes, and snippets.

@JamesIgoe
Last active August 23, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesIgoe/7e441be92b60931678bb7a2f8828db63 to your computer and use it in GitHub Desktop.
Save JamesIgoe/7e441be92b60931678bb7a2f8828db63 to your computer and use it in GitHub Desktop.
Filter for multiindex (2-column) series
def filterForLabels(df: pd.DataFrame, label) :
try:
sideLeft = df[label,]
except:
sideLeft = pd.DataFrame()
try:
sideRight = df[:,label]
except:
sideRight = pd.DataFrame()
if sideLeft.empty and sideRight.empty:
return pd.DataFrame()
elif sideLeft.empty:
concat = sideRight.to_frame()
concat.rename(columns={0:'Corr'},inplace=True)
return concat
elif sideRight.empty:
concat = sideLeft.to_frame()
concat.rename(columns={0:'Corr'},inplace=True)
return concat
else:
concat = pd.concat([sideLeft,sideRight], axis=1)
concat["Corr"] = concat[0].fillna(0) + concat[1].fillna(0)
concat.drop(columns=[0,1], inplace=True)
return concat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment