将具有两个数组列的数据框转换为数组列表
Convert dataframe with two array columns into list of arrays
我有一个数据框,其中包含两列,每个单元格中包含数组。下面是一些代码,用于创建一个与我的功能相同的小型示例数据框。
import pandas as pd
data = {'time': [
np.array(['2017-06-28T22:47:51.213500000', '2017-06-28T22:48:37.570900000', '2017-06-28T22:49:46.736800000']),
np.array(['2017-06-28T22:46:27.321600000', '2017-06-28T22:46:27.321600000', '2017-06-28T22:47:07.220500000', '2017-06-28T22:47:04.293000000']),
np.array(['2017-06-28T23:10:20.125000000', '2017-06-28T23:10:09.885000000', '2017-06-28T23:11:31.902000000'])
],
'depth': [
np.array([215.91168091, 222.89173789, 215.21367521]),
np.array([188.68945869, 208.23361823, 217.30769231, 229.87179487]),
np.array([169.84330484, 189.38746439, 178.91737892])
]
}
df = pd.DataFrame(data)
df
我想将数据绘制为三个单独的形状,每行一个,其中 time
值被视为 x 坐标,depth
值被视为 y 坐标。为此,我想制作一个看起来像这样的数组列表。
[array([['2017-06-28T22:47:51.213500000', 215.91168091],
['2017-06-28T22:48:37.570900000', 222.89173789],
['2017-06-28T22:49:46.736800000', 215.21367521], dtype=object),
array([['2017-06-28T22:46:27.321600000', 188.68945869],
['2017-06-28T22:46:27.321600000', 208.23361823],
['2017-06-28T22:47:07.220500000', 217.30769231],
['2017-06-28T22:47:04.293000000', 229.87179487], dtype=object),
array([['2017-06-28T23:10:20.125000000', 169.84330484],
['2017-06-28T23:10:09.885000000', 189.38746439],
['2017-06-28T23:11:31.902000000', 178.91737892], dtype=object)]
尝试使用 for 循环zip
l = [np.array(list(zip(x,y))) for x, y in zip(df.time,df.depth)]
Out[385]:
[array([['2017-06-28T22:47:51.213500000', '215.91168091'],
['2017-06-28T22:48:37.570900000', '222.89173789'],
['2017-06-28T22:49:46.736800000', '215.21367521']], dtype='<U29'),
array([['2017-06-28T22:46:27.321600000', '188.68945869'],
['2017-06-28T22:46:27.321600000', '208.23361823'],
['2017-06-28T22:47:07.220500000', '217.30769231'],
['2017-06-28T22:47:04.293000000', '229.87179487']], dtype='<U29'),
array([['2017-06-28T23:10:20.125000000', '169.84330484'],
['2017-06-28T23:10:09.885000000', '189.38746439'],
['2017-06-28T23:11:31.902000000', '178.91737892']], dtype='<U29')]
我有一个数据框,其中包含两列,每个单元格中包含数组。下面是一些代码,用于创建一个与我的功能相同的小型示例数据框。
import pandas as pd
data = {'time': [
np.array(['2017-06-28T22:47:51.213500000', '2017-06-28T22:48:37.570900000', '2017-06-28T22:49:46.736800000']),
np.array(['2017-06-28T22:46:27.321600000', '2017-06-28T22:46:27.321600000', '2017-06-28T22:47:07.220500000', '2017-06-28T22:47:04.293000000']),
np.array(['2017-06-28T23:10:20.125000000', '2017-06-28T23:10:09.885000000', '2017-06-28T23:11:31.902000000'])
],
'depth': [
np.array([215.91168091, 222.89173789, 215.21367521]),
np.array([188.68945869, 208.23361823, 217.30769231, 229.87179487]),
np.array([169.84330484, 189.38746439, 178.91737892])
]
}
df = pd.DataFrame(data)
df
我想将数据绘制为三个单独的形状,每行一个,其中 time
值被视为 x 坐标,depth
值被视为 y 坐标。为此,我想制作一个看起来像这样的数组列表。
[array([['2017-06-28T22:47:51.213500000', 215.91168091],
['2017-06-28T22:48:37.570900000', 222.89173789],
['2017-06-28T22:49:46.736800000', 215.21367521], dtype=object),
array([['2017-06-28T22:46:27.321600000', 188.68945869],
['2017-06-28T22:46:27.321600000', 208.23361823],
['2017-06-28T22:47:07.220500000', 217.30769231],
['2017-06-28T22:47:04.293000000', 229.87179487], dtype=object),
array([['2017-06-28T23:10:20.125000000', 169.84330484],
['2017-06-28T23:10:09.885000000', 189.38746439],
['2017-06-28T23:11:31.902000000', 178.91737892], dtype=object)]
尝试使用 for 循环zip
l = [np.array(list(zip(x,y))) for x, y in zip(df.time,df.depth)]
Out[385]:
[array([['2017-06-28T22:47:51.213500000', '215.91168091'],
['2017-06-28T22:48:37.570900000', '222.89173789'],
['2017-06-28T22:49:46.736800000', '215.21367521']], dtype='<U29'),
array([['2017-06-28T22:46:27.321600000', '188.68945869'],
['2017-06-28T22:46:27.321600000', '208.23361823'],
['2017-06-28T22:47:07.220500000', '217.30769231'],
['2017-06-28T22:47:04.293000000', '229.87179487']], dtype='<U29'),
array([['2017-06-28T23:10:20.125000000', '169.84330484'],
['2017-06-28T23:10:09.885000000', '189.38746439'],
['2017-06-28T23:11:31.902000000', '178.91737892']], dtype='<U29')]