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:
- Shows the assumptions that I made about the data types
- Allows my answer to be self contained (you don't have to go back to the question to build a complete working program)
- 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.
Python
+Panda
while "data frame"/"Data Frame" seem to be related toR
...