• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Repeat a set of numbers over a column pandas dataframe

Resolved: Repeat a set of numbers over a column pandas dataframe

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Let’s say we have two variables X and Y. I want to generate Y by repeating a set of numbers (1, 2, 3, 4, 5) or 1:5 over the rows. How can we generate column Y?
X   Y
20  1
95  2
86  3
95  4
9   5
19  1
4   2
26  3
66  4
72  5
26  1
5   2
2   3
73  4
73  5
88  1
51  2

Answer:

Use np.tile
>>> df['Y'] = np.tile(np.arange(1, 6), len(df))[:len(df)]

If you have better answer, please add a comment about this, thank you!

dataframe pandas python repeat
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.