Home > Enterprise >  Creating a range of numbers
Creating a range of numbers

Time:06-24

I'm trying to create a range of numbers in this dataframe:

pd.DataFrame({'Ranges': 1-np.arange(0, 1 , 0.1)
})

enter image description here

But the output expected is (in the same column):

0.9 - 1
0.8 - 0.9
0.7 - 0.8
0.6 - 0.7
0.5 - 0.6
0.4 - 0.5
0.3 - 0.4
0.2 - 0.3
0.1 - 0.2
0 - 0.1

I have tried using these enter image description here

CodePudding user response:

You can use string concatenation on the shifted Series:

df = pd.DataFrame({'Ranges': 1-np.arange(0, 1 , 0.1)})

s = df['Ranges'].round(2).astype(str)

out = s.shift(-1, fill_value='0.0')   ' - '   s

output:

0    0.9 - 1.0
1    0.8 - 0.9
2    0.7 - 0.8
3    0.6 - 0.7
4    0.5 - 0.6
5    0.4 - 0.5
6    0.3 - 0.4
7    0.2 - 0.3
8    0.1 - 0.2
9    0.0 - 0.1
Name: Ranges, dtype: object
  • Related