0

This is my starting dataframe:

headers = ['Title','GainLoss']
data = {'Title':['BMW','Geox','Bugatti','Toyota'],'GainLoss':[300,-87,658,-279]}
df = pd.DataFrame(data,columns=headers)

I want to separate the rowes with positive numbers from the rows with negative numbers and put them in two distinct dataframes. The following two lines of code may be the solution to the problem:

TitleGain = df['GainLoss'].agg(lambda x: x[x>0])
TitleLoss = df['GainLoss'].agg(lambda x: x[x<0])

The question is: In the two dataframes 'TitleGain' and 'TitleLoss' there is no column 'Title', but there must be!

How should I change my instructions?

Nepura
  • 125
  • 10

0 Answers0