Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign values to multiple non existing columns in a pandas dataframe?

So what I want to do is to add columns to a dataframe and fill them (all rows respectively) with a single value.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1,2],[3,4]]), columns = ["A","B"])
arr = np.array([7,8])

# this is what I would like to do
df[["C","D"]] = arr

# and this is what I want to achieve
#    A  B  C  D
# 0  1  2  7  8
# 1  3  4  7  8
# but it yields an "KeyError" sadly
# KeyError: "['C' 'D'] not in index"

I do know about the assign-functionality and how I would tackle this issue if I only were to add one column at once. I just want to know whether there is a clean and simple way to do this with multiple new columns as I was not able to find one.

like image 552
Dremet Avatar asked Mar 01 '18 14:03

Dremet


2 Answers

For me working:

df[["C","D"]] = pd.DataFrame([arr], index=df.index)

Or join:

df = df.join(pd.DataFrame([arr], columns=['C','D'], index=df.index))

Or assign:

df = df.assign(**pd.Series(arr, index=['C','D']))

print (df)
   A  B  C  D
0  1  2  7  8
1  3  4  7  8
like image 114
jezrael Avatar answered Sep 28 '22 03:09

jezrael


You can using assign and pass a dict in it

df.assign(**dict(zip(['C','D'],[arr.tolist()]*2)))
Out[755]: 
   A  B  C  D
0  1  2  7  7
1  3  4  8  8
like image 33
BENY Avatar answered Sep 28 '22 02:09

BENY