I have a pandas data frame df. I want df to be composed of 3 columns : the first one is a brand name (a string), the second is a list of integers, and the third one is a list of floats. So for each brand, I have two lists, and I want to put them all in a data frame to access different lists easily based on the brand name.
I have :
count = [1,5,198,0,0,35]
brand = 'Nike'
and to put the count list into the 'count' column corresponding to 'Nike' line I tried the following :
df[df['brand']==brand].loc[0,'count'] = count
df[df['brand']==brand]['count'] == count
df[df['brand']==brand]['count'].loc[0] == count
None of these would work and I get ValueError: Must have equal len keys and value when setting with an iterable
or A value is trying to be set on a copy of a slice from a DataFrame
and nothing changes in df.
How can I write a list into a pandas data frame cell ?
It seems to me that you are building a wrong data model. The model is not in 1st normal form (1NF) and you will have many troubles using it. Please, try to use a normalized model.
Brand price
0 Nike 50.0
1 Nike 60.0
2 Nike 70.0
3 Puma 30.0
4 Puma 100.0
You can get any computed value from this model with ease.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With