logo
down
shadow

Given a list of indices, modifying a subset of a pandas series at these indices


Given a list of indices, modifying a subset of a pandas series at these indices

By : Vinodkumar Bittiwar
Date : November 21 2020, 04:01 AM
seems to work fine You need to give the index of col1 as well, as df.iloc takes integer indices:
code :
>>> df.iloc[index, 0] = s1.values 
>>> df
      col1
0   1000.0
1      2.0
2   1000.0
3      3.0
4   1000.0
5      4.0
6   1000.0
7      5.0
8   1000.0
9      6.0
10  1000.0
11  1000.0
import pandas as pd
df = pd.DataFrame({'col1': [1000]*12})
s1 = pd.Series([i for i in range(2,7)])
index = [2*i+1 for i in range(5)] 
df.iloc[index, df.columns.get_loc('col1')] = s1.values


Share : facebook icon twitter icon
How can I delete rows/items of pandas series with indices' labels not in a list?

How can I delete rows/items of pandas series with indices' labels not in a list?


By : Caterina Centola
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can use Index.intersection with loc:
code :
s = pd.Series([1,2,5,6], index=list('abcd'))
print (s)
a    1
b    2
c    5
d    6
dtype: int64

L = list('cdef')
print (s.loc[s.index.intersection(L)])
c    5
d    6
dtype: int64
print (s.loc[s.index.isin(L)])
c    5
d    6
dtype: int64
How to return list of indices from pandas dataframe using another pandas series?

How to return list of indices from pandas dataframe using another pandas series?


By : M watson
Date : March 29 2020, 07:55 AM
hope this fix your issue I am using the kaggle house price dataset, and I have the following code which calculates interquartile ranges
code :
query = df.index[(df.sqft_area_binned == desiredBin) & (df.price > upperPriceBound)]
pandas series to_dict with repeated indices; make dict with list values

pandas series to_dict with repeated indices; make dict with list values


By : Adel Rubaian
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further If you have a neat pandas Series object with unique indices, then using pd.Series.to_dict() works as you might expect it: it becomes a Python dict with each index pointing to its respective value. , Try this:
code :
my_series.groupby(level=0).agg(list).to_dict()

Out[358]: {'1': ['val_1', 'val_2'], '2': ['val_3', 'val_4', 'val_5']}
Getting a list of indices where pandas boolean series is True

Getting a list of indices where pandas boolean series is True


By : Mike R
Date : March 29 2020, 07:55 AM
I wish this helpful for you Using Boolean Indexing
code :
>>> s = pd.Series([True, False, True, True, False, False, False, True])
>>> s[s].index
Int64Index([0, 2, 3, 7], dtype='int64')
>>> s[s].index.values
array([0, 2, 3, 7])
>>> np.nonzero(s)
(array([0, 2, 3, 7]),)
>>> np.flatnonzero(s)
array([0, 2, 3, 7])
>>> np.where(s)[0]
array([0, 2, 3, 7])
>>> np.argwhere(s).ravel()
array([0, 2, 3, 7])
>>> s.index[s]
array([0, 2, 3, 7])
>>> [*filter(s.get, s.index)]
[0, 2, 3, 7]
>>> [i for i in s.index if s[I]]
[0, 2, 3, 7]
How to find indices of nth occurance of a substring and subset the strings according to these indices in pandas

How to find indices of nth occurance of a substring and subset the strings according to these indices in pandas


By : anurag
Date : March 29 2020, 07:55 AM
Hope that helps I have a dataframe with a columns that contains strings and i want to slice it from second space to 4th space. In general, in a column of strings, How do i 1. find the indices of nth occurance of a substring 2. subset the strings according to these indices , Try:
code :
df['column'].str.split().str[2:4].str.join(' ')
Related Posts Related Posts :
  • Creating a Dataframe of Proportions
  • Scrapy with dynamic captcha
  • In python, how do I get urllib to recognize multiple lines in a string as separate URLs?
  • Add prefix and suffix to each element of a nested list
  • Generate string set from csv file in Python
  • Custom usage message for many-valued argument
  • Python Class, how to skip a wrong entry and proceed to next entry
  • Numpy efficient way to parse array of string
  • Kivy , Python: Update Label on_file_drop
  • What does it mean if a deeper conv layer converges first?
  • Selecting User in client.send_message() from arg list
  • python slicing multi levels list of dict using list comprehension
  • Value Error problem with multicell Dimensions must be equal, but are 20 and 13
  • How to print a board with coordinates?
  • Keras LSTM shape doesn't contain length of sequence
  • Boxplot with Pandas in Python
  • How can I rename a PySpark dataframe column by index? (handle duplicated column names)
  • How to calculate hash of a python class object
  • Using ideas from HashEmbeddings with sklearn's HashingVectorizer
  • keycloak.exceptions.KeycloakGetError: 404: b'' using Python 3.7
  • How to modify a column in a SQLite3?
  • VS Integration Services: flat file source to OLE DB destination - detect new data columns in file, add columns to table,
  • Customize xticks in matplotlib plot
  • How can I show the image in a labelframe which is inserted through askopenfilename?
  • Boxplot with distibution size histogram on top (and median regression)
  • Fit differential equation with scipy
  • ModuleNotFoundError: Correct setup
  • How to pass rendered plot to a html file through render_template?
  • Create flat ndarray from DataFrame column containing arrays
  • Bring radial axes labels in front of lines of polar plot matplotlib
  • Python3: Unable to split word from parsed data
  • Using Python to login to a website and web scrape
  • Customise shift in matplotlib offset
  • Combining and Reshaping rows and columns of 2 dataframes in R or Python
  • Regex condition after and before a known phrase
  • subplots based on records of two different pandas DataFrames ( with same structure) using Seaborn or Matplotlib
  • find numpy array in other numpy array
  • Print Triangle Pyramid pattern using for loop python
  • Python Script Running through Command Line Not Creating CSV
  • Questions about Subclassing
  • Creating list with dictionary instead of multiple dictionaries in python
  • Sorting queryset results in a template
  • Django Rest Framework allow not authenticated access to a view in certain cases
  • How do I efficiently map integers to URIs in a multidemnsional array?
  • Installing Python packages for Visual Studio Code
  • How to merge two columns into one in pandas dataframe
  • Decompose string of different symbols in python
  • pandas merge and group concat
  • How to traverse tree for making binary code from a HuffmanTree?
  • Check if IP is in network on Python3
  • non equally spaced points along x-axis in a plot
  • Concatenation of text files consisting list of lists?
  • Use regex to parse characters on a line of text
  • pandas df.fillna - filling NaNs after outer join with correct values
  • How can I undo a time series conversion of a pandas dataframe?
  • Virtual environment is not working in Django
  • FileNotFoundError in Python during Arabic text analysis
  • How to read email using python and smtplib
  • How to write a function which takes a string and turns into a single digit?
  • Linear Regression without Least Squares in sklearn
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org