Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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
Python 3.x 使用python分析特定组值的数据帧以重新组织数据帧_Python 3.x_Dataframe - Fatal编程技术网

Python 3.x 使用python分析特定组值的数据帧以重新组织数据帧

Python 3.x 使用python分析特定组值的数据帧以重新组织数据帧,python-3.x,dataframe,Python 3.x,Dataframe,假设我有一个数据帧,看起来像这样: id; year; methode 1; 2015; 2 2; 2015; 2 3; 2015; 2 12; 2013; 4 4; 2014; 7 1; 2014; 7 3; 2014; 7 id : {counts_of_diffrent_years : sum, years : [hit1,hit2], counts_of_diffrent_methods : 2, methods : [hit1,hit2]} {3 : {counts_o

假设我有一个数据帧,看起来像这样:

id; year; methode
1;  2015; 2
2;  2015; 2
3;  2015; 2
12; 2013; 4
4;  2014; 7
1;  2014; 7
3;  2014; 7
id : {counts_of_diffrent_years : sum, years : [hit1,hit2], counts_of_diffrent_methods : 2, methods : [hit1,hit2]}

{3 : {counts_of_diffrent_years : 2, years : [2014,2015], counts_of_diffrent_methods : 2, methods : [2,7]},...}
要实现这样的目标,最好的python方法是什么:

id; year; methode
1;  2015; 2
2;  2015; 2
3;  2015; 2
12; 2013; 4
4;  2014; 7
1;  2014; 7
3;  2014; 7
id : {counts_of_diffrent_years : sum, years : [hit1,hit2], counts_of_diffrent_methods : 2, methods : [hit1,hit2]}

{3 : {counts_of_diffrent_years : 2, years : [2014,2015], counts_of_diffrent_methods : 2, methods : [2,7]},...}

没有必要使用嵌套的dict,但我认为这是以后访问数据的最佳方式。有什么办法可以解决熊猫的问题吗?

我理解你想要解决的问题

从列中获取唯一值列表的步骤 唯一年份和方法的数量 对于第一个任务,请为列使用唯一方法,例如:

df.year.unique多年 df.methode.对于methodes是唯一的 了解更多

对于第二个任务,请执行下一步:

df.year.value_counts可计算年份的唯一计数 df.methode.value_计数计数方法的唯一计数
建议您熟悉

谢谢,但唯一值应仅为具有相同值的id生成,而不是为所有行生成。如果id=3在我的数据框中出现过几次,我只希望该id的年份和方法。然后对每个唯一的id重复它。好的,我建议您在条件计数>1的情况下使用groupby。df.groupby'year'。过滤器lambda x:x['year']。计数>1,检查本文