1

I am working on the Cars.csv DataFrame which can be found here: https://www.kaggle.com/ljanjughazyan/cars1

My goal is to create a new Data Frame with the column names: USA, Europe and Japan and to save the number of cars that are in each category.

for a in list(cars.origin.unique()):
    x= pd.DataFrame({a:[cars.loc[cars["origin"]==a,"origin"].size]})

I tried it with this code, but as a result I obtain a Data Frame with only one column that is "Europe". So it kind of works, but I cant figure why it just dismisses the other values. Why doesnt it work and can it be done using a for-loop?

Thanks in advance!!

1 Answer 1

2

I assume "Europe" would be the last item in your list. Because you are resetting x in every iteration of your for-loop.

So if you print(x) inside the loop, you should first see a DataFrame with just USA, then just Japan and then just Europe, which is your final result.

I would suggest putting the data into a python dict and creating you DataFrame afterwards.

data = {}
for a in list(cars.origin.unique()):
    data[a] = [cars.loc[cars["origin"]==a,"origin"].size]
x = pd.DataFrame(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.