简体   繁体   中英

Cannot subset the first column in a DataFrame

Im learning how to use Pandas and I've downloaded some data from Kaggle about car prices etc.

I'm trying to create a new dataframe by subsetting all the cars out that have the model "Golf".

golfs = df[df.model == "Golf"]

It does return a new dataframe but when i call it, its just empty besides the column names.

trying this:

others = df[df.model != "Golf"]

creates a new dataframe, but it has everything in it. The datatype for the column is an object. So i tried to create subsets by transmission, which is also an object.

man_trans = df[df.transmission == "Manual"]

creates a new data frame with solely Manual transmissions... I have no idea where its going wrong. I've tried subsetting all other columns but its just the first one that wont behave. Ive even tried copying and pasting the cell value directly into the code.

Ive even tried adding in:

df.reset_index()

to add in a new index as i thought that might be the problem.

The code looks like it is correct to me. If the golf dataframe is empty, it is possible you dont have any rows where df['model'] == 'Golf'?. Maybe it's =="golf" instead?

# It this doesnt work....
# golfs = df[df.model == "Golf"]
# Maybe try this (or something like this
golfs = df[df.model == "golf"]

So David was totally correct with the space, but it wasnt at the end it was at the beginning. I checked what David was saying by applying:

'''

len(df.model.iloc[798])

'''

To find out how many characters were in a cell to what I was looking for. "Golf" only has 4 but 5 was being returned.

'''

df.model = df.model.str.lstrip() len(df.model.iloc[798]) 4

'''

Thanks to the people that responded and helped me find an answer to this stupidly simple problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM