【问题标题】:How to add new column from array to Vaex dataframe after filtered?过滤后如何将新列从数组添加到 Vaex 数据框?
【发布时间】:2021-02-03 05:27:42
【问题描述】:

我有数据文件'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 代码

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)

我想将“e”列添加到df_filter,出现错误

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

我只关心长度为 3 的数据帧,因为我只是丢弃了无用的行。但不知何故,Vaex 希望我的长度为 5。

pandas 中的类似代码应该可以按预期运行

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)

【问题讨论】:

    标签: python vaex


    【解决方案1】:

    解决了

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-25
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 2020-11-17
      • 2020-05-08
      相关资源
      最近更新 更多