Home > other >  increment odd values and duplicate in pandas or excel
increment odd values and duplicate in pandas or excel

Time:12-06

Want an excel sheet column with odd values but has to be duplicated. The value of the column has to be like 1,1,3,3,5,5,7,7,9,9,11,11.... and so on until 20000/20001.

Could you let me know how to do it either in excel sheet or in pandas dataframe.

Tried duplicating in excel but it does not work. Tried in pandas but even even numbers come up. df['ABC'] = 2 df.index//1

CodePudding user response:

You can use numpy.arange with numpy.repeat :

import pandas as pd
import numpy as np

df = pd.Series(np.repeat(np.arange(1,20002,2),2)).to_frame("ABC")

Then (if needed) use pandas.DataFrame.to_excel to make a spreadsheet :

df.to_excel("out.xlsx", index=False)

# Output :

print(df)
         ABC
0          1
1          1
2          3
3          3
4          5
...      ...
19997  19997
19998  19999
19999  19999
20000  20001
20001  20001

[20002 rows x 1 columns]

CodePudding user response:

Using ms365, try something like:

=TOCOL(LET(x,SEQUENCE(10001,,,2),IFERROR(EXPAND(x,,2),x)))
  • Related