About 66,000 results
Open links in new tab
  1. pandas - Selection with .loc in python - Stack Overflow

    df.loc[['B', 'A'], 'X'] B 3 A 1 Name: X, dtype: int64 Notice the dimensionality of the return object when passing arrays. i is an array as it was above, loc returns an object in which an index with those values is returned. In this case, because j was a scalar, loc returned a pd.Series object.

  2. python - Why use loc in Pandas? - Stack Overflow

    Why do we use loc for pandas dataframes? it seems the following code with or without using loc both compiles and runs at a similar speed: %timeit df_user1 = df.loc[df.user_id=='5561'] 100 loops, best of 3: 11.9 ms per loop or %timeit df_user1_noloc = df[df.user_id=='5561'] 100 loops, best of 3: 12 ms per loop So why use loc?

  3. python - How are iloc and loc different? - Stack Overflow

    There are three different inputs you can use for .loc. A string; A list of strings; Slice notation using strings as the start and stop values; Selecting a single row with .loc with a string. To select a single row of data, place the index label inside of the brackets following .loc. df.loc['Penelope'] This returns the row of data as a Series

  4. python - Pandas use and operator in LOC function - Stack Overflow

    Jan 17, 2017 · i want to have 2 conditions in the loc function but the && or and operators dont seem to work.: df: business_id ratings review_text xyz 2 'very bad' xyz 1 '

  5. What is the difference between using loc and using just square …

    Select specific rows and/or columns using loc when using the row and column names. Select specific rows and/or columns using iloc when using the positions in the table. You can assign new values to a selection based on loc/iloc. I highlighted some of the points to make their use-case differences even more clear.

  6. python - pandas loc with multiple or conditions - Stack Overflow

    Jan 25, 2022 · I want to use loc and select only those rows where a value of certain is less than 0.5. I know I can do this as follows: df.loc[df.A < 0.5, :] and for multiple columns, I can do as follows: df.loc[(df.A < 0.5) | (df.B < 0.5) | (df.C < 0.5), :] My question is: Is there a better way to write conditions inside loc when you have more than 10 ...

  7. python - How to set column values using .loc and .contains - Stack …

    Apr 14, 2019 · Both the .loc and .contains functions return a dataframe object. The pandas documentation states that to reassign a value to each row in the column, I should use .loc, but when combined with .contains I get this warning: A value is trying to be set on a copy of a slice from a DataFrame.

  8. python - Replace values using loc command - Stack Overflow

    Jul 2, 2019 · As a side note, filtering the data with .loc and then using replace is redundant: .replace({512:263}) will convert values 512 only, no need to select that values before with .loc. If you do: df['Fare'].astype(int).replace({512:263}, inplace=True) you get the same result.

  9. python - df.loc more than 2 conditions - Stack Overflow

    Jan 17, 2019 · I know I can do this with only two conditions and then multiple df.loc calls, but since my actual dataset is quite huge with many different values the variables can take, I'd like to know if it is possible to do this in one df.loc call. I also tried np.where before, but found df.loc generally easier so it would be nice if I can stick with it.

  10. python - Using .loc with multiple selection criteria - Stack Overflow

    I want to run .loc to capture a subset of data that requires multiple criteria. Something like: df.loc[( df["date"] &gt; datetime.datetime.strptime('Jan 1 2000', '%b ...

Refresh