How to add new columns to vaex dataframe? Type Error

1.3k Views Asked by At

How to add new columns to vaex dataframe?

I received the type error when I try to assign a list object to the dataframe, as is done in pandas, but received following error:

ValueError: [1, 1, 1, 1, 1, 1, 1] is not of string or Expression type, but <class 'list'>
2

There are 2 best solutions below

0
On

Simple; convert list object to numpy array, and i guess that's what they define as expression type;

import numpy as np
a = [1]*7
a = np.array(a)
sub["new"] = a
sub

0
On

Let us first create a data frame using Vaex package:

import vaex
import numpy as np
x = np.arange(6)
y = x*2
df = vaex.from_arrays(x=x, y=y)
df

output:

#   x   y
0   0   0
1   1   2
2   2   4
3   3   6

Now, if you would like to add a new column called greeting:

df['greeting'] = ['hi', 'أهلا', 'hola', 'bonjour']

you will get this error:

ValueError: ['hi', 'أهلا', 'hola', 'bonjour'] is not of string or > Expression type, but <class 'list'>

To handle this problem, please use the following code:
output:

df['text'] = np.asanyarray(['hi', 'أهلا', 'hola', 'bonjour'])
df

#   x   y   text
0   0   0   hi
1   1   2   أهلا
2   2   4   hola
3   3   6   bonjour

Enjoy!