我有一个CSV文件,其中一列看起来像一个numpy数组。前几行看起来如下
<?php
$this->menu=array(
array('label'=>'List CvUpload', 'url'=>array('index')),
array('label'=>'Create CvUpload', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#cv-upload-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h2 align="center">Manage CV Upload</h2>
<?php }
$this->widget('booster.widgets.TbGridView', array(
'id'=>'cv-upload-grid',
'type' => 'striped bordered condensed',
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'columns'=>array(
'cv_id',
array('header'=>'Employee Name',
'value'=>'$data->user->user_id'),
array('name'=>'job_title_id',
'type'=>'raw',
'value'=>'CHtml::Link($data->jobTitle->name,array("JobTitle/view","id"=>$data->jobTitle->id))'),
'cv_type',
'version_id',
'upload_date',
'update_date',
'file_name',
'next_review_date',
'review_status',
'is_current',
array(
'header'=>'Actions',
'class'=>'CButtonColumn',
'template'=>'{view}{update}{down}',
'buttons'=>array(
'down' => array(
'label'=>'Download',
'imageUrl'=>Yii::app()->request->baseUrl.'/assets/d01d65b0/listview/down.gif',
'url'=>'$this->grid->controller->createUrl("download", array("new_url"=>$data->file_name))',
),
),
),),)
);
?>
<div class="modal"></div>
<div id="showdata"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/1.2.1/jquery-migrate.min.js"></script>
<script type="text/javascript">
$body = $("body");
$(document).on(
{
ajaxStart: function()
{
$body.addClass("loading");
},
ajaxStop: function()
{
$body.removeClass("loading");
}
});
$(document).ready(function(e)
{
$('input#view').live('click', function(e)
{
var dataId = $(this).parent().prev().prev().prev().prev().prev().prev().prev().prev().prev().text();//alert(dataId);
$.ajax({
type: 'POST',
data: {
id:dataId,
},
url: "<?php echo $this->createUrl("CvUpload/viewAjax"); ?>",
success: function(data, textStatus, jqXHR,html) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
if(textStatus=='success'){
$("#showdata").children().remove();
$("#showdata").append(data);
$('html, body').animate({
scrollTop: $("#showdata").offset().top
}, 1000);
}
},
error: function(textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
}
});
return false;
});
$('#cancel').live('click', function(e)
{
$("#showdata").children().remove();
});
});
</script>
当我使用pandas数据框加载此CSV并使用以下代码
时first,second,third
170.0,2,[19 234 376]
170.0,3,[19 23 23]
162.0,4,[1 2 3]
162.0,5,[1 3 4]
现在,我想基于“第一个&#39;专栏和联合第三个&#39;柱。所以在这样做之后我的数据框应该看起来像
data = pd.read_csv('myfile.csv', converters = {'first': np.float64, 'second': np.int64, 'third': np.array})
我如何实现这一目标?我尝试了多种方式,如下所示,似乎没有任何帮助实现这一目标。
170.0, [19 23 234 376]
162.0, [1 2 3 4]
答案 0 :(得分:1)
使用您当前的csv文件,第三个&#39;列以字符串形式出现,而不是列表。
可能有更好的方式转换为列表,但这里......
from ast import literal_eval
data = pd.read_csv('test_groupby.csv')
# Convert to a string representation of a list...
data['third'] = data['third'].str.replace(' ', ',')
# Convert string to list...
data['third'] = data['third'].apply(literal_eval)
group_data=data.groupby('first')
# Two secrets here revealed
# x.values instead of x since x is a Series
# list(...) to return an aggregated value
# (np.array should work here, but...?)
ans = group_data.aggregate(
{'third': lambda x: list(np.unique(
np.concatenate(x.values)))})
print(ans)
third
first
162 [1, 2, 3, 4]
170 [19, 23, 234, 376]