0

The main goal I am trying to accomplish is dividing these columns by each other, in python.

Okay so I have this excel file that I import called 'data', data has two columns that I need but they are in different tabs which is not a problem. I get both columns I need and name them the first is called 'city_area' it has 27 numbers in it ( it is printed out 0 - 26) the second column I retrieve is called 'population'. population has a gap at the top of the column before the numbers are listed. I use [6:] to get the data I actually need, population is then printed out and it has 27 numbers as well in it ( but these are listed 6 - 32). When I try to divide these two only the numbers that match up 6 - 26 are divided the rest result is NaN. Both are floats as well, any suggestions?

population1 = (df2['Unnamed: 5'])
population = population1[6:].astype(float)
population
6       664000.0
7      3557000.0
8       619000.0
9      3351000.0
10    13974000.0
11     8238000.0
12     2393000.0
13     3474000.0
14     5750000.0
15     6199000.0
16     2866000.0
17     2304000.0
18    19522000.0
19     7136000.0
20     3595886.0
21    10261856.0
22     8518000.0
23     3041000.0
24    15593000.0
25     3051000.0
26    10984000.0
27     1567000.0
28      405000.0
29     5974000.0
30    41164000.0
31     2007000.0
32     1337000.0

city_area = (df3['SQ_KM'])
city_area
0      8835.000
1       511.000
2      6407.000
3     11400.000
4       693.800
5       313.800
6      5802.000
7        93.380
8       739.000
9       827.141
10     3292.000
11     8096.000
12      330.900
13     1059.400
14      211.500
15      432.170
16      218.000
17     1390.000
18     1260.000
19      169.300
20      496.800
21    34091.000
22     5687.000
23      675.400
24     1520.000
25      181.857
26     2220.000
Name: SQ_KM, dtype: float64


answer = population/city_area
answer
0              NaN
1              NaN
2              NaN
3              NaN
4              NaN
5              NaN
6       114.443295
7     38091.668451
8       837.618403
9      4051.304433
10     4244.835966
11     1017.539526
12     7231.792082
13     3279.214650
14    27186.761229
15    14343.892450
16    13146.788991
17     1657.553957
18    15493.650794
19    42150.029533
20     7238.095813
21      301.013640
22     1497.802005
23     4502.517027
24    10258.552632
25    16776.918128
26     4947.747748
27             NaN
28             NaN
29             NaN
30             NaN
31             NaN
32             NaN
dtype: float64
rab
  • 67
  • 8
  • Any suggestions for what? I didn't get what your question is. – mkrieger1 Feb 15 '21 at 21:31
  • 1
    Code or it didn't happen - no, really. The description of what you think the code does is no substitute for an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). – Mr. T Feb 15 '21 at 21:32
  • 1
    What did you expect to be the result of dividing one number in the first column by no number in the second column? – mkrieger1 Feb 15 '21 at 21:32
  • I understand that there is no number in that column but how do I make it so that the '6' is equal to 1 and the rest follow 2,3,4 etc.. – rab Feb 15 '21 at 21:40
  • If i understood you right, you need both lists population and city_area have valid numbers at the same index starting at 0. I think slicing the list population with `population = population[6:33]` will solve your problem. – kindustrii Feb 15 '21 at 21:49
  • That is exactly what I am looking to do, this unfortunately did not work. – rab Feb 15 '21 at 21:55
  • 2
    It does this it divides numbers having the same index (the first column in your output). You want to [reset the index](https://stackoverflow.com/questions/20490274/how-to-reset-index-in-a-pandas-dataframe) of the columns and then divide. – Pranav Hosangadi Feb 15 '21 at 21:58
  • `population1 = (df2['Unnamed: 5'])` `population1 = population1[6:33]` `population = population1.astype(float)` What is the output of `population` after this three lines? – kindustrii Feb 15 '21 at 22:02
  • Got it! Just had to reset the index! perfect thanks! – rab Feb 15 '21 at 22:02

0 Answers0