Python通过一个特定的列值将多行分组为一个向量行

2024-04-25 21:35:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何按特定列值将多行分组为一个向量行。 例如,我有以下数据:

| Latitude  | Longitude  | group |
|-----------|------------|-------|
| 46.852397 | -72.02586  | A     |
| 47.059016 | -70.907962 | A     |
| 46.897785 | -71.140082 | A     |
| 46.99328  | -70.986152 | A     |
| 46.64613  | -71.934034 | A     |
| 46.622638 | -71.994857 | A     |
| 46.968093 | -71.284281 | B     |
| 47.422739 | -70.32361  | B     |
| 46.878963 | -71.717918 | B     |
| 46.91002  | -71.108395 | C     |
| 47.465175 | -70.337958 | C     |
| 46.6936   | -71.862257 | C     |
| 47.40885  | -70.390739 | C     |
| 47.00737  | -71.232117 | C     |
| 47.013901 | -70.965815 | C     |
| 46.824111 | -71.554997 | C     |
| 47.003765 | -71.193865 | C     |
| 46.665319 | -72.15102  | C     |
| 47.129865 | -70.842406 | C     |
| 46.932361 | -71.994677 | C     |

我想转换成:

| group | Latitude                                                                                                    | Longitude                                                                                                                 |
|-------|-------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| A     | [46.852397,47.059016,46.897785,46.99328,46.64613,46.622638]                                                 | [-72.02586,-70.907962,-71.140082,-70.986152,-71.934034,-71.994857]                                                        |
| B     | [46.968093,47.422739,46.878963]                                                                             | [-71.284281,-70.32361,-71.717918]                                                                                         |
| C     | [46.91002,47.465175,46.6936,47.40885,47.00737,47.013901,46.824111,47.003765,46.665319,,47.129865,46.932361] | [-71.108395,-70.337958,-71.862257,-70.390739,-71.232117,-70.965815,-71.554997,-71.193865,-72.15102,-70.842406,-71.994677] |

Tags: 数据group向量latitudelongitude列值
2条回答

你不能在你的配置中使用这样的东西吗:

<bean id="abstractControllerTemplate" abstract="true">
    <property name="validator" ref="myFormValidator"/>
</bean>
...
<bean id="someOtherConcreteController" class="com.myproj.controllers.SomeOtherConcreteController" parent="abstractControllerTemplate">
        <!  other properties specific to this controller  >
</bean>

您可以创建一个BeanPostProcessor来执行此操作,并在应用程序上下文中注册它。后处理器可以查找具有正确命名约定的AbstractController实例,生成验证器名称,通过反射实例化验证器对象,并在控制器中进行设置。比如:

public Object postProcessAfterInitialization(final Object bean, final String name) throws BeansException {
    if (bean instanceof AbstractController) {
        String controllerName = bean.getClass().getSimpleName();
        if(controllerName.endsWith("Controller")) {
            String validatorName = controllerName.replaceFirst("Controller$", "Validator");
            try {
                Class<?> validatorClass = Class.forName(validatorName);
                Validator validator = (Validator)validatorClass.newInstance();
                ((AbstractController)bean).setValidator(validator);
            } catch(Exception e) {
                throw new FatalBeanException("Cannot instantiate validator", e);
            }
        }
    }
    return bean;
}

或者,如果验证器注册为SpringBean,因为它们需要依赖项注入或其他什么,那么您可以创建一个BeanFactoryPostProcessor(而不是BeanPostProcessor),按类型或名称查找所有控制器bean定义,然后按类型或名称查找匹配的验证程序bean定义,并将匹配的验证程序添加到每个控制器bean的属性列表中。我没有这方面的示例代码,但希望您能理解

相关问题 更多 >