entercaspa
entercaspa

Reputation: 704

pandas wont add columns in order

I have two data frames

numbers:

Unnamed: 0                       Name  Number
42           42                   Aberavon    1742
43           43                  Aberconwy    2769
16           16             Aberdeen North    3253
25           25             Aberdeen South    4122
355         355         Airdrie and Shotts    1194
44           44                  Aldershot    4517 

and electorate:

    Unnamed: 0  Unnamed: 0.1                       Name  Number
0           533           533                   Aberavon   49821
1           534           534                  Aberconwy   45525
2           591           591             Aberdeen North   67745
3           592           592             Aberdeen South   68056
4           593           593         Airdrie and Shotts   66792
5             0             0                  Aldershot   72430

when I input

numbers['No. Voters] = electorate['Number']

for print(numbers) i get:

 Unnamed: 0                       Name  Number  No.Voters
42           42                   Aberavon    1742      80805
43           43                  Aberconwy    2769      78796
16           16             Aberdeen North    3253      68343
25           25             Aberdeen South    4122      66347
355         355         Airdrie and Shotts    1194      77534

which isobviously wrong, and I am not sure why, because index should not matter as they are in order of name anyway as I passed each through the sort_values function

can anyone tell me what is going wrong and what the correct command would be to match a new column in dataframe numbers to the number value in electorate?

Upvotes: 0

Views: 58

Answers (2)

jezrael
jezrael

Reputation: 862691

You can use values for converting column Number to numpy array, so align is corrected:

numbers['No. Voters] = electorate['Number'].values

Or reset_index of both DataFrames for correct align:

numbers.reset_index(drop = True)
electorate.reset_index(drop = True)
numbers['No. Voters] = electorate['Number']

Upvotes: 1

ysearka
ysearka

Reputation: 3855

Look at the indexes of your dataframes, they aren't the same. That's why you have problems when you create your new column.

If you don't care about keeping the index of electorate you can use reset_index before defining the new column of number:

electorate.reset_index(drop = True)

EDIT: Note that jezrael's solution with values is safer as it doesn't care about both indexes. Moreover it permits to keep the index of electorateif you need it later.

numbers['No. Voters'] = electorate['Number'].values

Upvotes: 1

Related Questions