Simd
Simd

Reputation: 21333

What is the correct way to add a list as a column to a dataframe?

I want to add a list as a new column to a dataframe. I am doing:

df['Intervention'] = interventionList

It gives me

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

I read Pandas add a series to dataframe column where the accepted answer is very similar.

Upvotes: 3

Views: 82

Answers (2)

Arjun Singh
Arjun Singh

Reputation: 1

You can try something like this

import pandas as pd
li = [1,2,3,4,5]
li2 =[6,7,8,9,10]
df = pd.DataFrame()

#Using pd.Series to add lists to dataframe
df['col1'] = pd.Series(li)
df['col2'] = pd.Series(li2)
df

Upvotes: 0

rhug123
rhug123

Reputation: 8768

I believe one option would be to use:

df.assign(Intervention = interventionList)

or to make a copy of the dataframe:

df2 = df.copy()

Upvotes: 1

Related Questions