Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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 按权重展开调查_Python_Python 3.x_Pandas_Numpy - Fatal编程技术网

Python 按权重展开调查

Python 按权重展开调查,python,python-3.x,pandas,numpy,Python,Python 3.x,Pandas,Numpy,我正在尝试扩大一项调查,通过它的重量、年份和id来进行一些回归。 我愿意使用以下数据帧 df id year weight X Y 1 2011 2 54 Medium 1 2012 1 57 Medium 2 2011 1 8 Micro 2 2012 2 10 Micro 3 2011 3 10 Micro 1

我正在尝试扩大一项调查,通过它的重量、年份和id来进行一些回归。 我愿意使用以下数据帧

df 
id    year   weight    X       Y
 1    2011        2   54  Medium
 1    2012        1   57  Medium
 2    2011        1    8   Micro
 2    2012        2   10   Micro
 3    2011        3   10   Micro
 1    2012        1    9   Micro
将其转换为类似的内容(示例只是一个示例,用于说明我的问题,我的真实数据集有很多ID和特性)

一种方法是使用:


我想你是在寻找过度采样
id    year   weight    X       Y
 1    2011        2   54  Medium
 1    2011        2   54  Medium
 1    2012        1   57  Medium
 2    2011        1    8   Micro
 2    2012        2   10   Micro
 2    2012        2   10   Micro
 3    2011        3   10   Micro
 3    2011        3   10   Micro
 3    2011        3   10   Micro
 1    2012        1    9   Micro
res = df.set_index(['id', 'year', 'X', 'Y'])['weight']\
        .repeat(df['weight'])\
        .reset_index()

print(res)

   id  year   X       Y  weight
0   1  2011  54  Medium       2
1   1  2011  54  Medium       2
2   1  2012  57  Medium       1
3   2  2011   8   Micro       1
4   2  2012  10   Micro       2
5   2  2012  10   Micro       2
6   3  2011  10   Micro       3
7   3  2011  10   Micro       3
8   3  2011  10   Micro       3
9   1  2012   9   Micro       1