Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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
Pandas 如何拆分数据帧中包含嵌套数组的单元格_Pandas_Jupyter Notebook - Fatal编程技术网

Pandas 如何拆分数据帧中包含嵌套数组的单元格

Pandas 如何拆分数据帧中包含嵌套数组的单元格,pandas,jupyter-notebook,Pandas,Jupyter Notebook,我有一个熊猫数据框,它包含610行,每行包含一个嵌套的坐标对列表,它看起来像: [1377778.480000004,6682395.37759999]是一个坐标对 我希望取消对每一行的测试,因此,不是一行包含坐标列表,而是每一个坐标对都有一行,即: 我已经尝试了这个问题中的s.apply(pd.Series).stack(),但不幸的是,没有成功 有什么想法吗?非常感谢 import pandas as pd import numpy as np data = np.arange(500

我有一个熊猫数据框,它包含610行,每行包含一个嵌套的坐标对列表,它看起来像:

[1377778.480000004,6682395.37759999]是一个坐标对

我希望取消对每一行的测试,因此,不是一行包含坐标列表,而是每一个坐标对都有一行,即:

我已经尝试了这个问题中的s.apply(pd.Series).stack(),但不幸的是,没有成功

有什么想法吗?非常感谢

import pandas as pd
import numpy as np

data = np.arange(500).reshape([250, 2])
cols = ['coord']

new_data = []
for item in data:
  new_data.append([item])

df = pd.DataFrame(data=new_data, columns=cols)

print(df.head())

def expand(row):
  row['x'] = row.coord[0]
  row['y'] = row.coord[1]

  return row

df = df.apply(expand, axis=1)
df.drop(columns='coord', inplace=True)
print(df.head())
结果
你可以做的一件事就是使用numpy。它允许您以快速高效的方式执行许多列表/数组操作。这包括“取消测试”(重塑)列表。然后,您只需转换为数据帧

比如说,

import numpy as np

#your list
coordinate_list = [[[1377778.4800000004, 6682395.377599999],[6582395.377599999, 2577778.4800000004], [6582395.377599999, 2577778.4800000004]]]

#convert list to array
coordinate_array = numpy.array(coordinate_list)
#print shape of array 
coordinate_array.shape

#reshape array into pairs of 
reshaped_array = np.reshape(coordinate_array, (3, 2))

df = pd.DataFrame(reshaped_array)
df.columns = ['X', 'Y']

输出将如下所示。如果我遗漏了什么,请告诉我

这是我对你问题的新答案。我使用“reduce”将嵌套数组展平,然后使用“itertoolschain”将所有内容转换为1d列表。之后,我将列表重塑为2d数组,允许您将其转换为所需的数据帧。我试着尽量通俗易懂。如果有任何问题,请告诉我

#libraries
import operator
from functools import reduce
from itertools import chain

#flatten lists of lists using reduce. Then turn everything into a 1d list using 
#itertools chain.
reduced_coordinates = list(chain.from_iterable(reduce(operator.concat, 
geometry_list)))

#reshape the coordinates 1d list to a 2d and convert it to a dataframe
df = pd.DataFrame(np.reshape(reduced_coordinates, (-1, 2)))
df.columns = ['X', 'Y']

您能添加一份可以复制粘贴的数据副本吗?我尝试从ArcGis获取这些坐标。它看起来是这样的:In:geometry \[u list=[]对于rslt-In-out['features']:gm=np.array(rslt['geometry']['rings'])geometry \[u list.append(gm)out:[array([[137777778.486682395.3776],[1377777777770.949682351.3558],…这是一个包含610个数组的列表,每个数组包含不同数量的坐标对,也以数组的形式呈现…当我尝试将列表转换为数组时,我得到以下错误:坐标数组=np。数组(几何体列表)值错误:无法将输入数组从形状(115,2)广播到形状(1)非常感谢!这很有效!我非常感谢:)实际上,它只适用于610个阵列中的一个(一个阵列看起来像[[1377909.0626 6696341.0589],[1377913.8088 6696326.7092],…]])。但是对于我的几何体列表,它已经是610个这样的阵列的列表,并且具有形状[array1([[…]])、array2([[…]]),…array610([[…]])它仍然不起作用……问题还在于这些数组的长度不同,所以我得到了ValueError:操作数不能与形状(1115,2)(1134,2)一起广播-array1由115个坐标对组成,array2由134个坐标对组成。好吧,我对你的建议做了一些修改,现在它可以工作了。))非常感谢:DDDhey,很高兴它现在能为你工作:)如果你喜欢的话,请随意给出我的答案并投赞成票
#libraries
import operator
from functools import reduce
from itertools import chain

#flatten lists of lists using reduce. Then turn everything into a 1d list using 
#itertools chain.
reduced_coordinates = list(chain.from_iterable(reduce(operator.concat, 
geometry_list)))

#reshape the coordinates 1d list to a 2d and convert it to a dataframe
df = pd.DataFrame(np.reshape(reduced_coordinates, (-1, 2)))
df.columns = ['X', 'Y']