0

This has already been asked, but I tried it in proper way, and somehow I am getting an unexpected result.

I have two dataframes data and dky with many columns.

The code,

data.loc[(data['source'] == 'DKY_2016'), 'seconds']

returns:

1147    22.80
1154    44.90
1160    45.00
1161    58.35
1162     2.45

I want to replace the aforementioned column with dky['seconds'], which contains:

0     41.22
1     22.80
2     44.90
3     45.00
4     58.35

I tried the following code data.loc[(data['source'] == 'DKY_2016'), 'seconds'] = dky['seconds'].

But it only giving the NaN as the output column

1147   NaN
1154   NaN
1160   NaN
1161   NaN
1162   NaN

What I am doing wrong here?

3
  • 2
    What if you try: data.loc[(data['source']=='DKY_2016'),'seconds']=dky['seconds'].values.tolist() Commented Nov 20, 2021 at 16:58
  • 1
    It worked, Still I didn't understood what was the problem! Commented Nov 20, 2021 at 17:00
  • 1
    It is because the indices are different. On data you have 1147, 1154, ... while on dky is 0,1,2,.. Commented Nov 20, 2021 at 17:01

1 Answer 1

2

This is because of indexes. dky['seconds'], as you can see, has an index of with 0 1 2 3 4. On the other hand, data.loc[(data['source'] == 'DKY_2016'), 'seconds'] has an index of 1147 1154 1160 1161 1162. Since those two are difference, pandas will not assign the one to the other.

Instead, you need to convert the data you're assigning, into a list first, so that it has no index:

data.loc[(data['source'] == 'DKY_2016'), 'seconds'] = dky['seconds'].values.tolist()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.