标签云

微信群

扫码加入我们

WeChat QR Code

Can someone explain how these three methods of slicing are different?I've seen the docs, and I've seen these answers, but I still find myself unable to explain how the three are different.To me, they seem interchangeable in large part, because they are at the lower levels of slicing.For example, say we want to get the first five rows of a DataFrame.How is it that all three of these work?df.loc[:5]df.ix[:5]df.iloc[:5]Can someone present three cases where the distinction in uses are clearer?


very important to mention the SettingWithCopyWarning scenarios: stackoverflow.com/questions/20625582/…and stackoverflow.com/questions/23688307/…

2019年10月21日25分57秒

Note that ix is now planned for deprecation:github.com/pandas-dev/pandas/issues/14218

2019年10月20日25分57秒

Great explanation! One related question I've always had is what relation, if any, loc, iloc and ix have with SettingWithCopy warnings? There is some documentation but to be honest I'm still a little confused pandas.pydata.org/pandas-docs/stable/…

2019年10月20日25分57秒

measureallthethings: loc, iloc and ix might still trigger the warning if they are chained together. Using the example DataFrame in the linked docs dfmi.loc[:, 'one'].loc[:, 'second'] triggers the warning just like dfmi['one']['second'] because a copy of data (rather than a view) might be returned by the first indexing operation.

2019年10月20日25分57秒

What do you use if you want to lookup a DateIndex with a Date, or something like df.ix[date, 'Cash']?

2019年10月21日25分57秒

cjm2671: both loc or ix should work in that case. For example, df.loc['2016-04-29', 'Cash'] will return all row indexes with that particular date from the 'Cash' column. (You can be as specific as you like when retrieving indexes with strings, e.g. '2016-01' will select all datetimes falling in January 2016, `'2016-01-02 11' will select datetimes on January 2 2016 with time 11:??:??.)

1970年01月01日00分02秒

In case you want to update this answer at some point, there are suggestions here for how to use loc/iloc instead of ixgithub.com/pandas-dev/pandas/issues/14218

2019年10月20日25分57秒

Is df.iloc[:, :]equivalent to all rows and columns?

2019年10月20日25分57秒

It is, as would be df.loc[:, :]. It can be used to re-assign the values of the entire DataFrame or create a view of it.

2019年10月20日25分57秒

Wow, this was one of the very well articulated and lucid explanations that i have ever come across of a programming topic, What you explained in the last about normal indexing which works either on row or columns is one of the reason we have loc and iloc method. I came across that caveat in the datacamp course. a.) What do df.columns and df.index return? Is it a list of strings? If it is a list, is it allowed to access two elements like this df.columns[ [2,4] ] in a list?b.) Can i call get_loc() on df.columns? c.) Why do we need to call df['age']>30.values in case of iloc.

2019年10月20日25分57秒