Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

summing multiple columns values which has different column names by using pattern matching of column names using pyspark/pandas

for example:
I have a dataframe with columns like

lens plain-prod 102 plain-prod 105 plain-prod 107
First 1 3 4
Second 2 5 3
First 3 7 2
Second 4 8 1

so i need to do a pattern matching (^plain-prod.*) and pick up all 3 columns matching that and create new column plain_sum having the sum how to achieve this using pyspark or pandas.

lens plain-prod 102 plain-prod 105 plain-prod 107 plain_sum
First 1 3 4 8
Second 2 5 3 10
First 3 7 2 12
Second 4 8 1 13

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this approach with Pandas (df is your data frame):

df['plain_sum'] = df.filter(regex='^plain-prod.*').sum(axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading