logo
Tags down

shadow

Replacing looks like range of elements in a column with a set of new values & setting the rest to be 0


By : Terrence McFadden
Date : October 17 2020, 08:10 AM
fixed the issue. Will look into that further From your previous question , using replace , about why it work , you can check link
code :
s=df.Borough.replace(dict(zip(l,[1,2,3,4,5])),regex=True)
pd.to_numeric(s,errors = 'coerce').fillna(0).astype(int)
Out[44]: 
0    3
1    5 # notice here still change to 5 
2    1
3    2
4    0
Name: Borough, dtype: int32
df = pd.DataFrame({
    'Borough': ['QUEENS', 'BRONX 777', 'MANHATTAN', 'BROOKLYN', 'INVALID']})
l = ['MANHATTAN', 'BROOKLYN', 'QUEENS', 'STATEN ISLAND','BRONX']


Share : facebook icon twitter icon

Change values in row based on a column value and replacing specified column range


By : Les Redfield
Date : March 29 2020, 07:55 AM
like below fixes the issue I found most of my answer with : Change values in row based on a column value r . There are two differences: I want to replace values based on test of a categorical variable and I want to specify the range for which I want the values to be replaced. I am working with a data frame of 8600 odd rows and 170 columns. , Try:
code :
x[x$Month %in% c("March", "April"), c("VAR2", "VAR3")] = NA

Replacing a Range of Column Values in R


By : Alparslan Fatih Tasa
Date : March 29 2020, 07:55 AM
seems to work fine Following the clarifications in the comments, this code should yield the expected output:
code :
SemStartWeek <- 5 #variable introduced to avoid obscure "magic numbers" in code
df1$Date <- paste("Week",as.numeric(strftime(df1$Date),"%W")) - SemStartWeek + 1)
#    Text   Date
#1 Text 1 Week 1
#2 Text 2 Week 2
#3 Text 3 Week 3
df1 <- structure(list(Text = structure(1:3, .Label = c("Text 1", "Text 2", 
               "Text 3"), class = "factor"), Date = structure(1:3, 
               .Label = c("2016-02-05 10:55:00", "2016-02-09 10:56:28", 
               "2016-02-18 20:40:33"), class = "factor")), .Names = c("Text",  
                "Date"), class = "data.frame", row.names = c(NA, -3L))

Replacing one column onto another in python/pandas, but keeping the replaced columns values if the replacing column has


By : Aloosh
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have two columns in a data frame that I want to merge together. The attached image shows the columns: , You can use df.apply.
code :
def get_new_val(x):
    if np.isnan(x.precio_uf_y):
       return x.precio_uf_x
    else:
       return x.precio_uf_y

df["new_precio_uf"] = df.apply(get_new_val, axis=1)

Setting or replacing values in a dataframe column based on other columns through a hash map


By : user2776986
Date : March 29 2020, 07:55 AM
it fixes the issue I finally found the solution and I share it here, in case it will be useful to others like me. I use a simpler code, to focus on the sintax. d is the mapping table (dictionary) and df is the table with columns A and B; column C is added with values based on A,B through the map d.
code :
In [12]: d
Out[12]: {(1, 1): 1, (1, 2): 2, (1, 3): 3, (1, 4): 4}

In [13]: df
Out[13]:
      A     B
0     1     2
1    11    22
2     1     3
3  1111  2222

In [14]: df['C'] = df[['A','B']].apply(tuple, axis=1).map(d)

In [15]: df
Out[15]:
      A     B    C
0     1     2  2.0
1    11    22  NaN
2     1     3  3.0
3  1111  2222  NaN
In [23]: d
Out[23]: {(1, 'a'): 1, (1, 'b'): 2, (1, 'c'): 3, (1, 'd'): 4}

In [24]: df
Out[24]:
      A     B
0     1     a
1    11    22
2     1     c
3  1111  2222

In [25]: df['C'] = df[['A','B']].apply(tuple, axis=1).map(d)

In [26]: df
Out[26]:
      A     B    C
0     1     a  1.0
1    11    22  NaN
2     1     c  3.0
3  1111  2222  NaN

In [27]:

Data Cleaning Python: Replacing the values of a column not within a range with NaN and then dropping the raws which cont


By : user2883833
Date : March 29 2020, 07:55 AM
it helps some times You can try this to solve your problem. Here, I tried to simulate your problem and solve it with below given code:
code :
import numpy as np
import pandas as pd


data = pd.read_csv('c.csv')
print(data)
data['A'] = data['A'].apply(lambda x: np.nan if x in range(1,10,1) else x)
data['B'] = data['B'].apply(lambda x: np.nan if x in range(10,20,1) else x)
data['C'] = data['C'].apply(lambda x: np.nan if x in range(20,30,1) else x)
print(data)
data = data.dropna()
print(data)
    A   B   C
0   1  10  20
1   2  11  22
2   4  15  25
3   8  20  30
4  12  25  35
5  18  40  55
6  20  45  60
      A     B     C
0   NaN   NaN   NaN
1   NaN   NaN   NaN
2   NaN   NaN   NaN
3   NaN  20.0  30.0
4  12.0  25.0  35.0
5  18.0  40.0  55.0
6  20.0  45.0  60.0
      A     B     C
4  12.0  25.0  35.0
5  18.0  40.0  55.0
6  20.0  45.0  60.0
import numpy as np
import pandas as pd


data = pd.read_csv('c.csv')
print(data)
data['A'] = data['A'].apply(lambda x: np.nan if x in (round(y,2) for y in np.arange(1.00,10.00,0.01)) else x)
data['B'] = data['B'].apply(lambda x: np.nan if x in (round(y,2) for y in np.arange(10.00,20.00,0.01)) else x)
data['C'] = data['C'].apply(lambda x: np.nan if x in (round(y,2) for y in np.arange(20.00,30.00,0.01)) else x)
print(data)
data = data.dropna()
print(data)
       A      B      C
0   1.25  10.56  20.11
1   2.39  11.19  22.92
2   4.00  15.65  25.27
3   8.89  20.31  30.15
4  12.15  25.91  35.64
5  18.29  40.15  55.98
6  20.46  45.00  60.48
       A      B      C
0    NaN    NaN    NaN
1    NaN    NaN    NaN
2    NaN    NaN    NaN
3    NaN  20.31  30.15
4  12.15  25.91  35.64
5  18.29  40.15  55.98
6  20.46  45.00  60.48
       A      B      C
4  12.15  25.91  35.64
5  18.29  40.15  55.98
6  20.46  45.00  60.48
Related Posts Related Posts :
  • Get mongod rs.status() results from a python script
  • ImportError: C extension: No module named 'parsing' not built
  • python pandas update column values related to previous updated row during iteration over it
  • 3 nested loops: Optimizing a simple simulation for speed
  • Assign subset of values to pandas dataframe with MultiIndex
  • How to group two sets of buttons on each top corner of the screen using Tkinter?
  • django login using class based for custom user
  • MRJob sort reducer output
  • Python Pandas Counts using rolling time window
  • Getting or editing a string from a column in a csv file with pandas
  • Python - Delete row in matrix/array if row contains
  • Using dicom Images with OpenCV in Python
  • Odoo ghost record
  • Creating and assigning multiple variables in a tkinter application
  • Graph dictionary
  • No changes to original dataframe after applying loop
  • AUC of Random forest model is lower after tuning parameters using hypergrid search and CV with 10 folds
  • Python: Reading multiple CSV files, and assigning each to a different variable
  • How to identify empty rectangle using OpenCV
  • How to iterate multilevel dataframe in python
  • How to limit the contour plot with a line plot?
  • Why subclassing a str or int behaves differently from subclising a list or dict?
  • Python decode with translation table
  • i need to click unordered links in the below URL using selenium, python
  • How to join pandas dataframe with itself?
  • How to apply a color cast to a video frame in OpenCV Python?
  • Is there any existing library for median filtering with kernel size greater then 5 using OpenCL acceleration in python?
  • Changing the color of points in scatter plot for different dummy values
  • Calculate center for each polygon in a list efficiently
  • Loading modules in the same Python package
  • replacing pixels in an imagewith pixels from another image python
  • Suggestion on picking the best options of two lists (minimum and maximum )python
  • Resetting Index in a Dataframe drops the Indexed column by 1 row
  • Convert number which are str from readlines to digits - python
  • Unable to authenitcate with python minds api
  • Print variables from a query in python
  • Ipython does not see the installed library
  • Javascript-like array-method chaining in Python?
  • PyQT: Get contents CustFormWidgetIem inside QListWidgetItem
  • Bottle server: HTTPResponse vs bottle.response
  • pytorch vgg model test on one image
  • Runtime scope and `main` symbol is different inside or outside a function
  • Use anaconda in pycharm (Import libraries error, updating anaconda and virtual environment)
  • how to get the sum of a CSV column list to print
  • Python plot drop lines with repeating value in column
  • receive binary file from POST request with BaseHTTPRequestHandler
  • D-Bus - 'ServiceUnknown' exception encountered while calling a remote procedure
  • Pandas .min() method doesn't seem fastest
  • Pandas: How to reference columns of structure: ('Name', n) ('Name', n+1)
  • Read a text file and remove all characters except alphabets & spaces in Python
  • Compute all powerset intersections of two lists
  • Applying literal_eval on string of lists of POS tags gives ValueError
  • Modelling a logic puzzle
  • What is the meaning of Copy_X in sklearn linear models
  • selenium.common.exceptions.ElementNotInteractableException: Message: Element is not displayed
  • pydev debugger does not stop in breakpoint
  • Python windows path regex
  • Flask and selenium-hub are not communicating when dockerised
  • How to use groupby on a single column and perform comparisons for multiple columns in Pandas?
  • Locate a python script without absolute path
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org