Skip to content Skip to sidebar Skip to footer

Elegant Way To Produce Description Of Columns Based On Column Data

I have a pandas dataframe: index data1 data2 1 30 20 2 20 10 3 40 90 I want to produce an array of descriptions, one for each row, i

Solution 1:

Use the dot trick:

df = pd.DataFrame({
    'data1': [30, 20, 40],
    'data2': [20, 10, 90]
}, index=[1,2,3])

df.gt(25).dot(df.columns + ' was significant\n').str.strip().tolist()
# ['data1 was significant', '', 'data1 was significant\ndata2 was significant']

Solution 2:

Or, using apply

In [323]: (df.gt(25).apply(lambda x: '\n'.join(
                       ['%s was significant' % c for c, v in x.iteritems() if v]), axis=1)
             .tolist())
Out[323]: ['data1 was significant', '', 'data1 was significant\ndata2 was significant']

Solution 3:

Or, use np.where.

[''.join(x) for x in np.where(df > 25, df.columns + ' was significant\n', '')] 
['data1 was significant\n',
 '',
 'data1 was significant\ndata2 was significant\n']

Post a Comment for "Elegant Way To Produce Description Of Columns Based On Column Data"