我想做一个pandas数据框,每一列都有特定数量的值。它将有四列:Gender
、Role
、Region
和一个名为Survey
的指示符变量。这些列的可能值分别为1-3、1-4、1-6和1或0。
我希望有11,725行,每列中每个值都有特定的数字(例如对于Gender
为261 × 1 m4n1x,其中Survey
为1
,对于Role
为2,392 × 1 m8n1x,其中Survey
为0
,等等)。我如何设置 Dataframe 以精确地拥有我想要的某些值的数量?这将基于Word中的此表,这里我基本上是旋转Characteristics
列,然后得到行数,因为这里每个单元格中都有值。我确信我可以使用numpy来实现这一点,但我不确定具体如何实现。
1条答案
按热度按时间1yjd4xko1#
您可以使用
numpy.repeat()
函数生成一个重复值数组,然后使用pd.concat()
将它们连接到Pandas Dataframe 中。下面是一个示例代码片段,应该可以实现您想要的功能:这段代码应该生成一个Pandas数据框,其中有11,725行,每列中的每个值都有指定的出现次数。