How to add new column from array to Vaex dataframe after filtered?

2.4k views Asked by At

I have data file 'for-filter.txt'

a,b,c,d
1,2,3,4
2,6,7,8
-1,2,3,4
4,5,5,5
-2,3,3,3

Vaex code that I am doing

import vaex as vx 
import numpy as np
df_vaex = vx.from_csv('for-filter.txt')
df_filter = df_vaex[df_vaex['a'] > 0]
df_filter.head()
df_filter['e'] = np.repeat("value", 3)

I want to add column "e" to df_filter, I got error

Array is of length 3, while the length of the DataFrame is 3 due to the filtering, the (unfiltered) length is 5. 

I just care about dataframe with length 3 because I just discard useless row. But somehow Vaex want me to have length 5.

The similar code in pandas should run as expected

import pandas as pd
import numpy as np

df_pd = pd.read_csv('for-filter.txt')
df_filter_pd = df_pd[df_pd['a'] > 0]
df_filter_pd.head()
df_filter_pd.loc[:, "e"] = np.repeat("value", 3)
1

There are 1 answers

0
Haha TTpro On BEST ANSWER

Solved

import vaex as vx 
import numpy as np
df_vaex = vx.from_csv('for-filter.txt')
df_filter = df_vaex[df_vaex['a'] > 0]
df_filter.head()
df_filter = df_filter.extract()  # add this line to make real dataframe from lazy indexing
df_filter['e'] = np.repeat("value", 3)