0

I'm sure this has been asked somewhere but I can't find it.. so apologies.

I have a dataframe: df and I want to say for column A and rows 0 to 10, give the values of x

but I need to do this in a loop.

I've used this code in my loop but it doesn't work. I know I need to use loc but can't see how to do columns and rows indexing

colname='A'
df[colname][:10]=x

This does work but it brings up that red box 100's of times, saying it's not a good way of doing it...

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy C:\Users\FSC05\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:18: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

Thanks in advance for your help.

Fred

1 Answer 1

1

With Pandas, you should look to avoid Python-level loops. Here, you can use iloc for integer positional indexing:

x = df.iloc[:10, df.columns.get_loc('A')]

iloc can be used to set as well as retrieve values:

x = 5
df.iloc[:10, df.columns.get_loc('A')] = x

This assumes, as should generally be the case, your column labels are unique.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for this. I'm really trying to get my head around indexing. Is anyone able to explain why my method didn't work? and the one above does?
Firstly, it's not clear what x is in your code. Secondly, chained indexing is explicitly discouraged in the docs (the link you've included), so never use syntax df[...][...]. Thirdly, you are assigning x to some kind of slice, but you want to do it the other way round, i.e. assign some result to a variable x.
ok. x is just a random number. So if I have a time series column 0,0,0,4,5,4 I'd like to change the first 2 rows to x, so the output would be x,x,0,4,5,4
@fred.Schwartz, Got it, iloc can be used to set and retrieve values, have updated to clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.