Question #802   Submitted by Answiki on 11/05/2021 at 06:19:50 PM UTC

How to add a list as a new column to an existing DataFrame in Python?

Answer   Submitted by Answiki on 11/05/2021 at 06:18:47 PM UTC

The simplest way to append a list as a new column to a Pandas DataFrame in Python is to use the syntaxe df[columnName]=list. Let's consider the following dataframe:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [4, 5]], columns = ["col1", "col2"])
   col1  col2
0     1     2
1     4     5

The following command appends a list to the DataFrame as a new column named col3:

>>> df['col3'] = [3, 6]
   col1  col2  col3
0     1     2     3
1     4     5     6
>>> df['col3'] = [3, 4]
   col1  col2  col3
0     1     2     3
1     4     5     4


8 events in history
Question by Answiki 11/05/2021 at 06:19:58 PM
How to add a list as a new column to DataFrame in Python?
Question by Answiki 11/05/2021 at 06:19:50 PM
How to add a list as a new column to an existing DataFrame in Python?
Question by Answiki 11/05/2021 at 06:19:35 PM
How to add a new column from a list to an existing DataFrame in Python?
Question by Answiki 11/05/2021 at 06:19:05 PM
How to append a list as a new column to a DataFrame in Python?
Question by Answiki 11/05/2021 at 06:18:57 PM
How to append a list as a new column to a Pandas DataFrame in Python?
Answer by Answiki on 11/05/2021 at 06:18:47 PM

The simplest way to append a list as a new column to a Pandas DataFrame in Python is to use the syntaxe df[columnName]=list. Let's consider the following dataframe:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [4, 5]], columns = ["col1", "col2"])
   col1  col2
0     1     2
1     4     5

The following command appends a list to the DataFrame as a new column named col3:

>>> df['col3'] = [3, 6]
   col1  col2  col3
0     1     2     3
1     4     5     6
>>> df['col3'] = [3, 4]
   col1  col2  col3
0     1     2     3
1     4     5     4


Answer by Answiki on 11/05/2021 at 06:18:10 PM

The simplest way to append a list as a column to a Pandas DataFrame in Python is to use the syntaxe df[columnName]=list. Let's consider the following dataframe:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [4, 5]], columns = ["col1", "col2"])
   col1  col2
0     1     2
1     4     5

The following command appends a list to the DataFrame as a new column named col3:

>>> df['col3'] = [3, 4]
   col1  col2  col3
0     1     2     3
1     4     5     4


Question by Answiki 11/05/2021 at 06:10:47 PM
How to append a list as a column to a Pandas DataFrame in Python?
# ID Query URL Count

Icons proudly provided by Friconix.