-1

i am using python to code in jupyter notebook.

Im trying to use pandas to split a column of dataframe (called "PostTypeId' into two separate dataframes, based on the columns value - one dataframe is to be called Questions and has the column value of 1, and the second dataframe is to be called Answers that has the column value of 2. Im asked to do all this by defining it within a function, called split_df Wondering how i would go about this.

Thanks so much:)

Lily K
  • 1
  • 1
  • Please take the [tour](https://stackoverflow.com/tour) and take look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) In particular, please add a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) (also look [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) that replicates your problem. – Timus Aug 22 '22 at 09:40

1 Answers1

0

you can do it by:

Questions=pd.DataFrame(PostTypeId[PostTypeId.col_name==1])
Answers=pd.DataFrame(PostTypeId[PostTypeId.col_name==2])

when creating the function, use the filter values as the argument

  • Thanks so much, would this look correct? def split_df(df): arrow_to_df(Posts.feather) df = pd.posts_df('PostTypeId') Questions=pd.DataFrame(PostTypeId[PostTypeId.col_name==1]) Answers=pd.DataFrame(PostTypeId[PostTypeId.col_name==2]) return Questions, Answers – Lily K Aug 22 '22 at 06:16
  • No, try to generalize the function so that you can feed arguments into the function. Like def split_df(df, value_filter): return pd.DataFrame(df[df.col_name==value_filter]) – Irsyaduddin Aug 22 '22 at 06:30