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

how to map df by dict inside dict

iam tryin to change values on specific row by check if value of one col is in my dict . this is my…
matan
  • 451
  • 4
  • 12
0
votes
1 answer

Pandas .loc[] method is too slow, how can I speed it up

I have a dataframe with 40 million rows,and I want to change some colums by age = data[data['device_name'] == 12]['age'].apply(lambda x : x if x != -1 else max_age) data.loc[data['device_name'] == 12,'age'] = age but this method is too slow, how…
0
votes
1 answer

Passing a string to .loc accessor pandas

I have a dataframe which is multi-index and passing a list or a string to the loc accessor doesn't work. My dataframe has two indexes : Name_1, Name_2. Let tempList=[ 'Marco John' , Peter Dorset'] data.loc['Marco John','Peter Dorset']['Combined…
KBS
  • 1
  • 1
0
votes
1 answer

Calling data frame values by index name

I have this data-frame: index name a b 1-1 1 2 1-2 2 4 5-1 3 6 5-2 4 8 7-1 5 4 7-2 6 5 I want to call only values starting with 'index name' = 5 index name a b 5-1 3 6 5-2 …
Jack_T
  • 91
  • 1
  • 9
0
votes
1 answer

Find descriptor in csv file then give it a value

Very new here, as in started yesterday with everything. I am working on a way to pull out one of four categories of weather from this csv file. VFR, MVFR, IFR, LIFR are the categories which every row in the csv should have. import pandas as pd ap1…
Nick H
  • 205
  • 2
  • 9
0
votes
1 answer

Pandas - add column with row value applying conditions relative to current row

I’m trying to add a new column to my dataframe that contains the time value of the first instance where the tick is equal to the current tick plus 1. df2 is somthing like this: Time Tick Desired col Count 0 …
0
votes
1 answer

Error in pandas.loc calculation - Copy of a sliced dataframe

I have two pandas dataframes, one named df1 and another named df2: import pandas as pd df1 = pd.DataFrame({'PERCENTAGE': [0.35,0.1105,0.0487,0.98],}) df2 = df1.loc[df1['PERCENTAGE'] > 0.4] I'm trying to create a new column in df2 using this…
Caldass_
  • 82
  • 6
0
votes
1 answer

Pandas Dataframe slice fillna value not being assigned (not working)

I tried this values = {'BsmtQual':'None','BsmtCond':'None', 'BsmtExposure':'None', 'BsmtFinType1':'None', 'BsmtFinType2':'None'} df_test.loc[:, ('BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2')].fillna(value=values,…
Ameer Ul Islam
  • 380
  • 3
  • 5
0
votes
1 answer

Unable to update nan values in column using loc?

I have stored the index values of all records where the column had missing values in index and I'm trying to impute nan values from another dataframe on the basis of 'StockCode' column common in both dataframes. This is how df_mod looks like. df is…
sahil
  • 3
  • 3
0
votes
1 answer

How to apply iloc in a Dataframe depending on a column value

I have a Dataframe with the follow columns: "Country Name" "Indicator Code" "Porcentaje de Registros" (as it is show in the image) for each country there are 32 indicator codes with its percentage value. The values are order in an descending way,…
0
votes
0 answers

Divide df.loc by df.loc with NaN values; error 'str' / 'str'

I am working with a large csv file (1.8 GB) and am trying to divide two variables (rows) by each other to create a new variable. It works for some variables; however, not for all. The code is as described below: #get the variables out df1 =…
0
votes
3 answers

pandas error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I have a dataframe. distdf=pd.DataFrame(dist) for i in range(len(distdf)): if distdf.loc[i]==distdf.loc[i+1]: print("true") else: print('no') but I have a error. ValueError: The truth value of a Series is ambiguous. Use…
lincplus htm
  • 95
  • 2
  • 7
0
votes
0 answers

Assign value to new df column after multiple matching conditions with other dataframe

Assume we have two DataFrames: DF1: spec u_g target G1 4.8 0.88 G2 2.1 0.76 WG2 1.4 0.71 WG2 1.2 0.68 WG2 1.0 0.52 WG3 0.8 0.65 WG3 0.7 0.53 SWG3 0.7 0.31 DF2: id type u_g_1 1 WG2 …
Aukru
  • 135
  • 6
0
votes
1 answer

ValueError: The truth value of a Series is ambiguous while using lambda with loc

Df.loc[lambda Df: Df['score'] > 15 and Df['score'] < 20] I am getting the mentioned error while using the above-mentioned code. Thanks in advance Error : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any()…
akash bais
  • 27
  • 3
0
votes
1 answer

Using .loc in division of columns calculation

I'm looking to do the following calculation in a way that prevents a warning error from python: My table has the following example data setup for one row: a b c d e f g 5 2 4 3 1 2 5 df['d'] = ((df['d']/df['c']))*100 As you can see, I'm looking…
AndrewK
  • 27
  • 6