I am trying to add a new column with some values in my dataframe using pandas and have it repeat the same values until it reaches the end of the index:
I have tried:
df['Fruit Type']=['Bananas','Oranges','Strawberries']
it says:
ValueError: length of values does not match length of index
**My index is about 8000 rows long, so there is a mismatch between index and the number of new column values
I want the column to look like:
Fruit Type:
Bananas
Oranges
Strawberries
Bananas
Oranges
Strawberries
Bananas
Oranges
Strawberries
I found a solution after a while:
df.insert(0, 'Fruit Type', ['Bananas', 'Oranges','Strawberries']*int(((len(df))/3)))
The 0 stands for column number, followed by column name, then column values. The *int...takes the index divided by 3 and repeats the values for that amount. Thanks to @acai for the multiplier at the end
TypeError: can only concatenate list (not "int") to list.