13

I'm fairly new to Stack Overflow and am unsure what the limits are to editing. I have read over Stack Overflow's documentation on edits, and read over some meta posts on editing, but could not find the answer to my question. That is, can you add a data frame to a question where one is described, or is that too much of an inference?

Example of OP's question:

I have a dataframe where the entries are in the form "Jane Doe" and the column name is "GenericNames". I would like to only keep the first name... [etc]

Is it allowed or recommended to edit the question to add something like:

#Example df
df=pd.DataFrame({"GenericNames":['Jane Doe', 'John Doe', 'Foo Bar', 'Hello World']})

OR

    GenericNames
0   Jane Doe
1   John Doe
2   Foo Bar
3   Hello World

Thank you for your input.

2
  • 1
    Mini-Rmk/Sugg: The Qt would be "clearer" if you added to which Prog Lang(s)/Tag(s) you are referring to, + choosing only 1 Spelling between "data frame"/"dataframe"/"DataFrame", as "dataframe"/"DataFrame" seem (from 'Google Search') to be related to Python + Panda while "data frame"/"Data Frame" seem to be related to R...
    – chivracq
    Oct 22, 2021 at 14:24
  • 4
    It's the OP's responsibility to add examples of his problem.
    – Poul Bak
    Oct 23, 2021 at 15:12

1 Answer 1

40

As someone who answers a fair amount of pandas questions I would recommend against creating sample data to add to the question.

If the question does not include any sample data, and is asking for some operation over unknown data it is ambiguous and needing clarification.

In these cases, I comment with either a reference to the tag wiki or directly to the canonicals:

In many cases, I also vote to close for Needing Details or Clarity.


The reason I do this is because I have seen more than enough questions wherein the question is something like:

I have a column that contains a list of names. How do I count the length of each list?

From reading this I assume:

df = pd.DataFrame({'names': [['Jane', 'Jim'], ['Mike', 'Mary']]})

    names
0   ['Jane', 'Jim']
1   ['Mike', 'Mary']

However in reality their data actually looks like:

df = pd.DataFrame({'names': ['Jane, Jim', 'Mike Mary']})

    names
0   Jane, Jim
1   Mike Mary

Notice there is a significant difference between these two things. One is a column of strings, and one is a column of lists. The solutions for these are very different as there are different methods available for list manipulation and string manipulation.

If I introduce either of these into the question, I will skew all of the subsequent answers to the question which can be very problematic if the assumptions I made are incorrect. The asker is responsible for providing the information to make the question answerable. They are the only ones who know the specific problem they are facing. Even if we're correct most of the time, there is still potential for error in doing so.


The only times I might add code constructors/text data to a question is if op makes a question like:

I would like the sum the values in col, here is my dataframe

I have on occasion replaced the image to make the question:

I would like to sum the values in col. Here is my DataFrame:

df = pd.DataFrame({'col': [1, 2, 3, 4]})

      col
0     1
1     2
2     3
3     4

I am hesitant to do so in general, however, for a similar reason as above. Data is lost in creating the image. Specifically the dtypes are hard to determine from the image alone. I am making assumptions when it is much easier for the asker to just run df.to_clipboard() or df.to_records() or df.to_dict() themselves.

The other case where I have added data to the question is when the asker answered their question instead of editing which resulted in the data being included in a deleted answer and not the question, or when the asker uses the comments to include sample data. In both of these cases, I'm not making data; I am moving data into a more accessible format.


My general approach to answers on questions where even the provided data can have some ambiguity (DataFrame provided as text or image) is to also include my setup at the end of my answer here are some examples: example 1 and example 2. In the corresponding questions, both dataframes were supplied as text.

Including the DataFrame constructors in my answer does 3 things for me:

  1. Shows the assumptions that I made about the data types
  2. Allows my answer to be self contained (you don't have to go back to the question to build a complete working program)
  3. It allows me to easily come back to and rebuild solutions in response to comments or when methods change.

This is my personal style, everyone has different approaches to making their answers as clear as possible.

5
  • 4
    I'd add another example against doing this: sometimes the question is non-reproducible but we'd only know that from an actual data sample. At least in the R tag, we get tons of questions on "why doesn't this date-related function work on my column of dates?", and the reason is that the data turns out to not actually be date objects. If we took the OP at their word that they have actual dates, not strings of them, we'd miss that entirely and try to answer a question that's off-topic
    – camille
    Oct 22, 2021 at 16:14
  • The first line is unclear: I would recommend against creating sample data to add to the question. If the question does not include any sample data, What do you mean by sample data? The code sample or table sample?
    – TheMaster
    Oct 24, 2021 at 10:40
  • So, the TL;DR version: 1. Yes, OP shall add sample data to reproduce. 2. Yes, if OP linked to external source, then edit to add the minimum data needed to reproduce. 3. No, anyone else shouldn't come up with sample data themselves. Oct 24, 2021 at 11:41
  • @TheMaster I don't understand what you mean by "code sample" / "table sample". "Sample data," in the context of both the pandas tag wiki and the canonicals, refers exclusively to a small reproducible sample DataFrame which can come in the form of text or a code constructor. So if you're differentiating the DataFrame code constructor and the DataFrame text display then there is no difference and I mean both. If by "code sample" you mean like making up an attempt to add to the question like an MRE, I would also so don't do this, but I didn't think that's what this question was asking.
    – Henry Ecker Mod
    Oct 24, 2021 at 14:40
  • 1
    @ThomasWeller I would say that (2) might be a "Maybe" more than a "Yes". But otherwise an accurate summary.
    – Henry Ecker Mod
    Oct 24, 2021 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .