我有一个数据框数据:
Stretch
SB
NB
...
WB
那里的行有Nan。我有二维列表:
[['SB', someFloatvalue, someFloatvalue], ['SB', someFloatvalue, someFloatvalue], ['NB', someFloatvalue, someFloatvalue]][['NB', someFloatvalue, someFloatvalue], ['SB', someFloatvalue, someFloatvalue], ['NB', someFloatvalue, someFloatvalue]]
上面的每个2D列表都与Stretch中的每一行相关。我想删除所有与Strecth值不同的列表。最后, 预期结果是:
[['SB', someFloatvalue, someFloatvalue], ['SB', someFloatvalue, someFloatvalue]][['NB', someFloatvalue, someFloatvalue], ['NB', someFloatvalue, someFloatvalue]]
说明:Stretch的第一行是SB。因此, 相应的2D列表仅应在其中列出SB。依此类推… 如果在拉伸中使用Nan, 则只需按原样显示列表即可。
我的想法是将Stretch转换为元组, 因为我认为该职位很重要。它没有解决..我不知道如何映射它们。
#1你可以尝试在列表理解中使用zip():
out = [[l for l in sub_data if l[0] == key or str(key) == "nan" ] for sub_data, key in zip(data, df.Stretch)]
或通常使用以下循环:
out_1 = []
for sub_data, key in zip(data, df.Stretch):
sub = []
for l in sub_data:
if l[0] == key or str(key) == "nan":
sub.append(l)
if len(sub) >
0: out_1.append(sub)
【Python如何比较数据框列和2D列表()】完整代码
df = pd.DataFrame({"Stretch": ["SB", "NB", np.NaN]})data = http://www.srcmini.com/[[['SB', 123456, 123456], ['SB', 123456, 123456], ['NB', 123456, 123456]], [['NB', 123456, 123456], ['SB', 123456, 123456], ['NB', 123456, 123456]], [['NB', 123456, 123456], ['SB', 123456, 123456], ['NB', 123456, 123456]]]# Usual for loop
out_1 = [[l for l in sub_data if l[0] == key or str(key) == "nan"] for sub_data, key in zip(data, df.Stretch)]
print(out_1)
# [[['SB', 123456, 123456], ['SB', 123456, 123456]], #[['NB', 123456, 123456], ['NB', 123456, 123456]], #[['NB', 123456, 123456], ['SB', 123456, 123456], ['NB', 123456, 123456]]]out_2 = []
for sub_data, key in zip(data, df.Stretch):
sub = []
for l in sub_data:
if l[0] == key or str(key) == "nan":
sub.append(l)
if len(sub) >
0: out_2.append(sub)print(out_2)
# [[['SB', 123456, 123456], ['SB', 123456, 123456]], #[['NB', 123456, 123456], ['NB', 123456, 123456]], #[['NB', 123456, 123456], ['SB', 123456, 123456], ['NB', 123456, 123456]]]print(out_1 == out_2)
# True
推荐阅读
- 如何从JSON中提取嵌套的值()
- tuple()增加运行时间这么多是否正常()
- 使用JMESPath根据另一个查询结果过滤列表
- Python Pandas ffill if语句问题()
- Pip已自行卸载,但我不知道是什么原因造成的
- 通过Python转义HTML中的单引号()
- Python中的Altair(数据可视化)
- 2020年11个最佳iOS开发框架热门推荐
- 如何实现android的翻转动画(已添加GIF)