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
2 answers

Is there a faster way to fill in a new column in a dataframe based on exsiting ones than using loc in python?

I have a dataframe that includes the total number of products made in a factory for every day but it is a cumulative field and not daily values. I am trying to calculate daily values by just subtracting every day's cumulative number from next day's…
molosoco
  • 9
  • 1
0
votes
1 answer

How do I get a block of Python Pandas .loc lines to reference the original dataframe rather than running in order on top of one another?

In the example below (df name 'statement', column name 'product'), I want product 1 to be changed to product 2. I achieve this on the first line, but then the second line changes it from product 2 to product 18. Later, it changes from product 18 to…
jray
  • 1
0
votes
3 answers

pandas get the min/max value of a row in a dataframe of only those rows that contain a certain string in another column

I feel really stupid now, this should be easy. I got good help here how-to-keep-the-index-of-my-pandas-dataframe-after-normalazation-json I need to get the min/max value in the column 'price' only where the value in the column 'type' is buy/sell.…
duhh
  • 53
  • 1
  • 5
0
votes
1 answer

combining loc code lines into 1 function python pandas

I have some code which cleans up browser names to short names in a pandas dataframe column e.g. 'Edge 12345678' becomes 'Edge'. Because I want to account for future versions of browsers (the version number will change) I haven't used a dictionary. I…
Mizz H
  • 67
  • 6
0
votes
3 answers

Concatenating Zero to every row that has len() == 1.0 using loc[] methd

I have a set of data with one column: New_Value 0 51 1 45 2 1 3 78 4 0 5 78 csv files do not represent the zero found after a digit. 10 becomes 1 and this presents inaccuracy in what I'm trying to do. I want to add a zero to every…
Moshe
  • 107
  • 1
  • 9
0
votes
1 answer

Dataframe loc with parameter str

I have an interface which gets a str with the condition to be executed. Is it possible to directly pass this str in my pandas loc? example: df: {'col A': [1, 2, 3, 4], 'col B': ['a', 'b', 'c', 'd']} entry_str = "col A == 2" df = df.loc[entry_str]
Eolynas
  • 19
  • 5
0
votes
2 answers

pandas conditional loc append values

I have the following df d = {'col1': [1, 2], 'col2': [3, 4], 'col3':["dog", "cat"]} df = pd.DataFrame(data=d) col1 col2 col3 0 1 3 dog 1 2 4 cat If I wanted to change the value in col3 in rows which col1=1 and col2=3 to fox I…
pyassign67
  • 35
  • 1
  • 5
0
votes
2 answers

Need to extract or remove columns from python

I have a list that looks like this: categorical_features = \ ['FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond', 'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1', 'BsmtFinType2',…
Johnny
  • 819
  • 1
  • 10
  • 24
0
votes
0 answers

Using loc with a list from a DatetimeIndex returns error

When I create a DatetimeIndex object with date_range() and feed list of datelabels to loc[[ ]] an error is returned. I never had any problem with a list of indexlabels in loc. Simple code example (python 3 in Jupyter Notebook): import pandas as…
0
votes
1 answer

Average of specific rows and columns using loc[] in Pandas

Here I am trying to pull average of rows 0 through 8 and columns "9/15/2020" through "9/18/2020" from df dataframe using .loc[] in pandas [This is the df dataframe having 8 rows and 9 columns][1] [1]: https://i.stack.imgur.com/QgjBp.png Have tried…
0
votes
1 answer

how to select partial dataframe by datetimeindex?

I have a Dataframe with a datetimeindex and a datetimeindex and i need to select only the rows from the datetimeindex. how to do? ix = pd.DatetimeIndex(['2019-07-18 00:00:00', '2019-07-18 00:01:00', '2019-07-18 00:02:00', '2019-07-18 00:03:00',…
La-Li-Lu-Le-Low
  • 191
  • 2
  • 15
0
votes
0 answers

Using dataframe.loc with multiple strings

relatively new to python. I am trying to use the df.loc function to index a column in my dataframe. I want to return the rows in the dataframe where this column equals a number of strings. Using df.loc to index a column where the column is being…
0
votes
2 answers

If str1 in row of col==str in row of another col, set str1=str2 of third col

I have a merged dataframe in which I would like to check each string contained in the row of the 'profile' column against each string in the row of the 'right_side' col. If these two strings are equal, I would like to replace the str in the…
Erin
  • 465
  • 4
  • 11
0
votes
0 answers

can't save information on Pandas dataframe with loc

I would really appreciate if someone can explain why this is happening. I am iterating over a dataframe and saving some information I am gathering online. The information doesn't save if I use df.loc[i]['column']; however, if I use df.column[i], it…
Magdalena
  • 1
  • 3
0
votes
1 answer

Getting AttributeError: 'str' object has no attribute loc

infections = [] for i in range(0, 20): read_nii(raw_data.loc[i,'infection_mask'], infections, 'infections') On executing this code, I am getting AttributeError: 'str' object has no attribute 'loc'.How can I resolve this error?