Last active
August 23, 2020 17:27
-
-
Save JamesIgoe/7e441be92b60931678bb7a2f8828db63 to your computer and use it in GitHub Desktop.
Filter for multiindex (2-column) series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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