Questions tagged [pandas-loc]

For questions about the [pandas] loc indexer. This tag should only be used for questions primarily about pandas loc or the behavior of this indexer. Be sure to also include the [pandas] tag on your question.

The pandas loc indexer allows for label based data selection from pandas DataFrames on multiple axes simultaneously.

It is primarily used in data Selection by label and Setting data at specific locations in a pandas DataFrame.

There are loc indexers for both DataFrames and Series.

381 questions
0
votes
1 answer

using dictionary to replace words in sentence in DataFrame

I am trying to replace words in a sentence in dataframe by a dictionary. How can I replace the original dataframe? The dictionary : rep_vocab contains {wrong words: correct words} dataframe: data_test column['question1'] the sentences column['d5']…
0
votes
1 answer

Change column value with Dask DataFrame loc

I have a huge database, which I need to change the value of a column according to a certain condition. In Pandas I execute the following code to accomplish what I want: df.loc[ (df['ID_CRITERIO_APURACAO'] ==…
0
votes
0 answers

How to sum data based on a boolean amount?

Created dictionary called items. Combined values for different keys and put into variable food_list. items={'Food':['Ice Cream','Salad'],'Computer':['Laptop','Notebook'] food_list= '|'.join(items['Food']) Description Amount Lenovo…
Nick
  • 111
  • 2
  • 12
0
votes
1 answer

DataFrame: To find a specified row and assign a numeric value to another column in the same row

When the conditional expression, c1 = 2017-04-01 holds, we want to substitute the number 300.0 for c3. As a result, 300.0 is substituted for r3 and c3. However, the conditional expression is not r3 columns: c1 = 2017-04-01 is r3 to be organized, so…
saru999
  • 7
  • 4
0
votes
1 answer

How to use a variable with multiple possible formats in loc to slice a pandas dataframe with a DatetimeIndex

I need to define a function that will perform several operations on a dataframe containing a DatetimeIndex. One of these operations is to slice the dataframe based on a period or date passed as one of the function arguments. When using loc within a…
Javgs
  • 43
  • 6
0
votes
1 answer

Does loc in pandas use vectorised logic or a for loop?

I access rows in pandas with the loc function as below: pdf.loc[pdf.a>2] Is this vectorised? Is it better than using numpy pdf[pdf.a>2]
Chogg
  • 389
  • 2
  • 19
0
votes
1 answer

How to use the std function on section of a pandas dataframe?

I am trying to slice the array up when using the std function to only use part of it. That part being 1:t, as t is an integer that grows with each iteration of the for loop. Y is my pandas dataset and the column is 'TempK' which is specified as the…
0
votes
1 answer

How to set column values using .loc and .contains

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…
Bugbeeb
  • 2,021
  • 1
  • 9
  • 26
0
votes
0 answers

Is there an elegant way using loc to reduce a dataframe with arbitrary 'OR' statements?

Let's say have a giant dataframe documenting the number of animals in a zoo [Animal] ... [number] [cow] ... [3] [fish] ... [6] . . . [pig] ... [5] and I want to reduce this to look at n specific animals then I would do something like…
0
votes
1 answer

Conditional between duplicated values through different columns

A customer is duplicated when it has more than one subscription. I want to generate a new_status for the customers status as a whole and not for each of subscriptions: to a customer which has Reactivated the subscription and to a customer which has…
0
votes
1 answer

'float' object is not subscriptable / from putting float into dataframe

value_temp = p.split(';') value = round(float(value_temp[2]),5) for i in range(24): df_1.loc[i] = [1,date[0],value[i]] print(df_1) I split a string. What I want to do is extract the values from string and change it to float, and then put it…
김경수
  • 1
  • 1
0
votes
2 answers

assign new value to repeated (or multiple) objective element(s) to a pandas dataframe

I have a pandas dataframe: df = pd.DataFrame({'AKey':[1, 9999, 1, 1, 9999, 2, 2, 2],\ 'AnotherKey':[1, 1, 1, 1, 2, 2, 2, 2]}) I want to assign a new value to a specific column and for each element having a specific value in that column. Let say…
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
0
votes
1 answer

Group By with sumproduct

I am working with a df with the following structure: df = DataFrame({'Date' : ['1', '1', '1', '1'], 'Ref' : ['one', 'one', 'two', 'two'], 'Price' : ['50', '65', '30', '35'], 'MktPrice' : ['63', '63', '32', '32'], …
Rose
  • 203
  • 2
  • 10
0
votes
0 answers

Setting dataframe columns returns persisting "value is trying to be set on a copy of a slice" warning

I am aware of the implications of chaining get/set and the fact that it may mean I am working on a copy. If I use loc(), I still get a warning (without the loc part, but still a warning). I have a DF with a column 'A' which is a date but with a…
Alex
  • 39
  • 5
0
votes
2 answers

How to lag data by x specific days on a multi index pandas dataframe?

I have a DataFrame that has dates, assets, and then price/volume data. I'm trying to pull in data from 7 days ago, but the issue is that I can't use shift() because my table has missing dates in it. date cusip price price_7daysago 1/1/2017 …
Dick Thompson
  • 599
  • 1
  • 12
  • 26