apply
How does require() in node.js work?
Source code is here. exports/require are not keywords, but global variables. Your main script is wrapped before start in a function which has all the globals like require, process etc in its context. Note that while module.js itself is using require(), that’s a different require function, and it is defined in the file called “node.js” … Read more
Count occurrences of items in Series in each row of a DataFrame
You could apply value_counts: In [11]: df.apply(pd.Series.value_counts, axis=1) Out[11]: C1 C2 C3 None 0 1 NaN NaN 2 1 1 1 NaN 1 2 2 NaN NaN 1 3 1 1 1 NaN So you can fill the NaN and applend just the base values you want: In [12]: df.apply(pd.Series.value_counts, axis=1)[[‘C1’, ‘C2’, ‘C3’]].fillna(0) Out[12]: C1 … Read more
Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas
OK, two steps to this – first is to write a function that does the translation you want – I’ve put an example together based on your pseudo-code: def label_race (row): if row[‘eri_hispanic’] == 1 : return ‘Hispanic’ if row[‘eri_afr_amer’] + row[‘eri_asian’] + row[‘eri_hawaiian’] + row[‘eri_nat_amer’] + row[‘eri_white’] > 1 : return ‘Two Or More’ … Read more
pandas apply function that returns multiple values to rows in pandas dataframe
Return Series and it will put them in a DataFrame. def myfunc(a, b, c): do something return pd.Series([e, f, g]) This has the bonus that you can give labels to each of the resulting columns. If you return a DataFrame it just inserts multiple rows for the group.
Sorting rows alphabetically
t(apply(DF, 1, sort)) The t() function is necessary because row operations with the apply family of functions returns the results in column-major order.