python - 根据 pandas Dataframe 中的索引从列表中删除元素

标签 python python-2.7 pandas numpy dataframe

如何根据 pandas Dataframe 中的索引范围从列表中删除元素。

假设 DataFrame 是这样的

df:

    values           size
0  [1,2,3,4,5,6,7]    2     #delete first 2 elements from list
1  [1,2,3,4]          3     #delete first 3 elements from list
2  [9,8,7,6,5,4,3]    5     #delete first 5 elements from list

预期输出是

df:

    values           size
0  [3,4,5,6,7]        2
1  [4]                3
2  [4,3]              5

最佳答案

将列表理解与索引结合使用:

df['values'] = [i[j:] for i, j in zip(df['values'], df['size'])]
print (df)
            values  size
0  [3, 4, 5, 6, 7]     2
1              [4]     3
2           [4, 3]     5

关于python - 根据 pandas Dataframe 中的索引从列表中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496684/

相关文章:

python - 如何在 Python 中读取图像的像素数

python - 如何在 python 中使用 diffLib 避免输出文件中出现公共(public)行

python - 如何从 python 中的 raw_input 制作列表?

python-2.7 - TypeError : 'PathCollection' object is not iterable when adding second legend to plot

python - 通过组合文本 pandas 制作新的数据框

python - 在一个巨大的 Panda DataFrame 中将几列放在前面

python - 我如何在不在 python 中转储的情况下解析 wiki 页面?

python - 在keras自定义层中进行广播的逐元素乘法

python - 如何在 python 中解码无效的 json 字符串

python - 如何转换为 ctypes 中的常量?