sample_comments
sample_comments(
data=None,
*columns,
n=SAMPLE_SIZE,
min_chars=MIN_CHARS,
max_chars=MAX_CHARS,
exclude=None,
by_column=True,
by=None,
seed=None,
)Pick a random sample of open-text comments.
Pulls a tidy, presentation-ready sample of comments out of one or more free-text columns, applying the usual clean-ups: drop blanks and very short answers, optionally exclude comments matching unwanted terms, and truncate to a maximum length. With by_column=True you get n from each source column (e.g. an even split across nps_com and show_com); with False, n from the pooled set. The result feeds plot_quotes_tree(). For a set that is varied rather than uniformly random, use sample_comments_diverse().
Python draws different random numbers from R, so the same seed picks different comments than it does in R, equally at random.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pandas.DataFrame | A data frame. If omitted, the session default is used. | None |
| *columns | str or selector | One or more comment columns, e.g. “nps_com”, “show_com”, or ends_with("_com"). |
() |
| n | int | Number of comments to sample. With by_column=True, this is per column. |
SAMPLE_SIZE |
| min_chars | int | Drop comments with this many characters or fewer. Default 30. | MIN_CHARS |
| max_chars | int | Truncate longer comments to this length. Default 400. | MAX_CHARS |
| exclude | str or list of str | Case-insensitive regular expressions; comments matching any are dropped (e.g. ["friend", "recommend"]). |
None |
| by_column | bool | If True (default), sample n from each column; if False, sample n from the pooled comments. |
True |
| by | str | Grouping column, e.g. an NPS group. Sampling then takes n from each group, and the group is kept as a column of the result. |
None |
| seed | int | Seed for reproducible sampling. Without one, numpy’s shared random generator is used (see numpy.random.seed()). |
None |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.DataFrame | source, comment and length, plus the by column when one is given. |
See Also
sample_comments_diverse, plot_quotes_tree
Examples
>>> sample_comments(podracing_survey, "nps_com", "show_com", n=3)
>>> grouped = podracing_survey.assign(group=nps_group(podracing_survey["nps_value"], labels=True))
>>> sample_comments(grouped.dropna(subset=["group"]), "nps_com", n=3, by="group")