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

Add column in pandas vectorized

i have a Term-Frequency matrix saved as a pandas dataframe.

     1000  Merkwürdig  Mindestens  Error  ...  Periode  bildet  30  Button
0       0           0           0      0  ...        0       0   0       0
1       0           1           0      2  ...        0       0   0       0
2       0           0           0      0  ...        0       0   0       0
3       0           0           0      0  ...        0       0   0       0
4       0           0           1      0  ...        0       0   1       0
..    ...         ...         ...    ...  ...      ...     ...  ..     ...
121     0           0           0      0  ...        0       0   0       1
122     0           0           0      0  ...        0       0   0       0
123     0           0           0      0  ...        0       0   0       0
124     0           0           0      0  ...        0       0   0       0

For each row i want to count the word occurence, add a column called ‘count’ at the end, and save the wordcount for each row.

     1000  Merkwürdig  Mindestens  Error  ...  Periode  bildet  30  Button    count
0       0           0           0      0  ...        0       0   0       0        0
1       0           1           0      2  ...        0       0   0       0        3
2       0           0           0      0  ...        0       0   0       0        0

Iterating over each row and column is probably not the best soution, so could this be vectorized?

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 :

You can use .sum method

df['count'] = df.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