Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列设置为NumPy数组_Python_Arrays_Pandas_Numpy_Dataframe - Fatal编程技术网

Python 将列设置为NumPy数组

Python 将列设置为NumPy数组,python,arrays,pandas,numpy,dataframe,Python,Arrays,Pandas,Numpy,Dataframe,假设我有一个NumPy数组: x = np.array([0, 1, 1, 3, 4, 0, 5, 2, 2, 1]) 以及数据帧: df = pd.DataFrame({'start': [2, 5, 1, 0, 0], 'stop': [6, 9, 4, 3, 2]}) # start stop # 0 2 6 # 1 5 9 # 2 1 4 # 3 0 3 # 4 0 2 start和s

假设我有一个NumPy数组:

x = np.array([0, 1, 1, 3, 4, 0, 5, 2, 2, 1])
以及数据帧:

df = pd.DataFrame({'start': [2, 5, 1, 0, 0], 'stop': [6, 9, 4, 3, 2]})

#    start  stop
# 0      2     6
# 1      5     9
# 2      1     4
# 3      0     3
# 4      0     2
start
stop
列对应于NumPy数组
x
中的开始和停止索引。因此,我想在数据框中添加第三列,它表示
x
中的序列(对象)。我可以使用
iterrows
来完成此操作:

df['sequence'] = [[] for _ in range(len(df))]
for idx, row in df.iterrows():
    df.at[idx, 'sequence'] = x[row['start']:row['stop']]

#    start  stop      sequence
# 0      2     6  [1, 3, 4, 0]
# 1      5     9  [0, 5, 2, 2]
# 2      1     4     [1, 1, 3]
# 3      0     3     [0, 1, 1]
# 4      0     2        [0, 1]

但是,对于具有数百万行的数据帧,
iterrows
变得不受欢迎。我需要一个既快速又不需要消耗大量内存的解决方案。

我们不使用
zip
和for-loop执行Itrows如何

[x[s:t]for s , t in zip(df.start,df.stop)]
[array([1, 3, 4, 0]), array([0, 5, 2, 2]), array([1, 1, 3]), array([0, 1, 1]), array([0, 1])]
#df['sequence'] = [x[s:t]for s , t in zip(df.start,df.stop)]

我们不使用
zip
和for循环执行Itrows怎么样

[x[s:t]for s , t in zip(df.start,df.stop)]
[array([1, 3, 4, 0]), array([0, 5, 2, 2]), array([1, 1, 3]), array([0, 1, 1]), array([0, 1])]
#df['sequence'] = [x[s:t]for s , t in zip(df.start,df.stop)]
你可以这样做

>>> x = np.array([0, 1, 1, 3, 4, 0, 5, 2, 2, 1])
>>> df = pd.DataFrame({'start': [2, 5, 1, 0, 0], 'stop': [6, 9, 4, 3, 2]})
>>> df['sequence'] = [x[df['start'][idx]:df['stop'][idx]] for idx in range(len(df))]
>>> df

#   start  stop      sequence
# 0      2     6  [1, 3, 4, 0]
# 1      5     9  [0, 5, 2, 2]
# 2      1     4     [1, 1, 3]
# 3      0     3     [0, 1, 1]
# 4      0     2        [0, 1]
你可以这样做

>>> x = np.array([0, 1, 1, 3, 4, 0, 5, 2, 2, 1])
>>> df = pd.DataFrame({'start': [2, 5, 1, 0, 0], 'stop': [6, 9, 4, 3, 2]})
>>> df['sequence'] = [x[df['start'][idx]:df['stop'][idx]] for idx in range(len(df))]
>>> df

#   start  stop      sequence
# 0      2     6  [1, 3, 4, 0]
# 1      5     9  [0, 5, 2, 2]
# 2      1     4     [1, 1, 3]
# 3      0     3     [0, 1, 1]
# 4      0     2        [0, 1]

如果需要长度(形状)不同的列表或数组,则很可能需要迭代。快速的全数组操作生成多维数组,而不是“参差不齐”的数组。只要
序列的每一行
都是
x
的切片/视图,内存消耗就不会很大。但请记住,很难“保存”这样的数据帧。如果您需要长度(形状)不同的列表或数组,则很可能需要迭代。快速的全数组操作生成多维数组,而不是“参差不齐”的数组。只要
序列的每一行
都是
x
的切片/视图,内存消耗就不会很大。但请记住,要“保存”这样的数据帧是很困难的。事实证明,这非常快@slaw很高兴我能帮上忙:-)这真是太快了@slaw很高兴我能帮忙:-)