Created
December 6, 2019 14:05
-
-
Save jeremy-rutman/b7b9ec707edf22eb525492c50e4391f1 to your computer and use it in GitHub Desktop.
change a column based on other column values, possibly using other column values as result
This file contains hidden or 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
| import pandas as pd | |
| df = pd.read_excel('soxl.xlsx') | |
| df=df[df['IDs of phase known'].notnull() | df['Ids of distance known'].notnull()] | |
| #If you wanted to remove any row with any missing data you can use the builtin : | |
| df = df.dropna() | |
| #which removes any row with missing values (which otherwise get imported as Nan values). | |
| #If you want to replace the 2nd col. with master column values when they are missing, you can do | |
| df = df.where((pd.notnull(df)), None) | |
| #to replace the NaN's with None (useful in the next step) and then | |
| df['IDs of phase known']= df.apply(lambda r:r['IDs of phase known'] if r['IDs of phase known'] else r['Set of Ids known for transformers'],axis=1) | |
| #And of course you can do the same with the 3rd col. If you want to replace only in the case where both col2 and 3 values are missing, you can do similarly, but check both columns for None: | |
| df['IDs of phase known']= df.apply(lambda r:r['Set of Ids known for transformers'] if not (r['IDs of phase known'] or r['Ids of distance known']) else r['IDs of phase known'] ,axis=1 ) | |
| #You can also do this, actually a bit easier, with numpy: | |
| df['IDs of phase known'] = np.where(np.isnan(df['IDs of phase known']), df['Set of Ids known for transformers'], df['IDs of phase known']) | |
| #If you want to just replace missing values with master column vals, or | |
| df['IDs of phase known'] = np.where( (np.isnan(df['IDs of phase known']) & (np.isnan(df['Ids of distance known']))),df['Set of Ids known for transformers'],df['IDs of phase known']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment