Blake
Blake

Reputation: 41

Pandas loc is returning series not df

The following code returns a series for y when I want a df. Ultimately I am pulling rows out of a larger raw df (df) to create a smaller df (Cand) of results. I have created Cand as the new empty df to be populated.

Cand = pd.DataFrame(columns=['SR','Hits','Breaks'])

x = df.loc[df['Breaks'] == 0]
y = x.loc[x['Hits'].idxmax()]

Cand.append(y)

x is correctly reflected as a df, but y becomes a series and so does not populate Cand.

I have looked around but cannot find a similar problem. Thanks in advance.

Upvotes: 1

Views: 1550

Answers (1)

qzdl
qzdl

Reputation: 48

Your issue would not be that you aren't passing a DataFrame to append(), but that .append() here is not in-place; try reassigning the return of append() to Cand as Cand = Cand.append(y), given that append returns your initial DataFrame + other (Cand + y, in this case).

Side Note:

You can return a DataFrame from .loc by using double square brackets.
Example: y = x.loc[[x['Hits'].idxmax()]]

Upvotes: 2

Related Questions