logo
down
shadow

change a block of row values in a column in one go


change a block of row values in a column in one go

By : Akhil
Date : November 19 2020, 03:01 PM
this one helps. I wanted to change a contiguous block of row values in one column in one go.
code :
df["my_col_name"]='yes'
df.loc[df.index>=4,"my_col_name"]='no'
df['new1']=np.where(df.index>=4,'no','yes')


Share : facebook icon twitter icon
How to change values in a column based on cell values in another column in excel?

How to change values in a column based on cell values in another column in excel?


By : souser16
Date : March 29 2020, 07:55 AM
will be helpful for those in need I read about if condition but apparently it only applies to single cell of two columns. I was wondering if there is way to modify the entire column based on cell values from another column. For example , Have a table in Sheet2 as follows.
code :
=================  ================
       A                   B
=================  ================
      Delhi              India
      New York           USA
      Hong Kong          China
How to change values in pandas database column based on values in same column?

How to change values in pandas database column based on values in same column?


By : kaiser
Date : March 29 2020, 07:55 AM
wish helps you You can calculate weeks which have at least one Output equal to "Yes". Then use pd.DataFrame.loc to update values:
code :
# get array of weeks in scope
yes_weeks = df.loc[df['Output'] == 'Yes', 'Week'].unique()

# update values conditionally
df.loc[df['Week'].isin(yes_weeks), 'Output'] = 'Yes'
Pandas groupby: change values in one column based on values in another column

Pandas groupby: change values in one column based on values in another column


By : user2006765
Date : March 29 2020, 07:55 AM
this will help I'll try to explain my problem as best as possible. But I'm new to Pandas, so please bear with me. I have a Pandas dataframe df: , IIUC, you can using index assign after groupby
code :
s=df.groupby('Random_ID').tail(1).loc[lambda x : x.Type=='Agent'].index
s
Out[62]: Int64Index([4, 14], dtype='int64')
df.loc[s,'SeqToken']='Agent_Final'
df
Out[64]: 
    Random_ID  Seq_ID   Type     SeqToken
0           8       1   User        First
1           8       2  Agent       Second
2           8       3   User       Second
3           8       4   User       Second
4           8       5  Agent  Agent_Final
5          13       1   User        First
6          13       2  Agent       Second
7          13       3   User       Second
8          13       4  Agent       Second
9          13       5   User       Second
10         13       6  Agent       Second
11         13       7   User       Second
12         13       8  Agent       Second
13         13       9   User       Second
14         13      10  Agent  Agent_Final
data.table: How to change column values based on grouped unique row values that contain column names

data.table: How to change column values based on grouped unique row values that contain column names


By : mark.picodigital
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a data.table with ~18^6 rows and I need to take the unique values of CLASS, by ID, and set their respective columns to 1, as seen below in a baby example , Using dcast and merge you could also do:
code :
DT <- data.table::data.table(ID=c("1","1","1","2","2"),
                             CLASS=c("a","a","b","c","b"),
                             a=c(0,0,0,0,0),
                             b=c(0,0,0,0,0),
                             c=c(0,0,0,0,0))

# dcast to convert to wide
DT_dcast <- dcast(DT[, .(ID, CLASS)], ID ~ CLASS, fun.aggregate = function(x) length(unique(x)), value.var = "CLASS")
DT_dcast
   ID a b c
1:  1 1 1 0
2:  2 0 1 1

# Then merge with the original data.table
DT_m <- merge(DT[, .(ID, CLASS)], DT_dcast, by = "ID")
DT_m
   ID CLASS a b c
1:  1     a 1 1 0
2:  1     a 1 1 0
3:  1     b 1 1 0
4:  2     c 0 1 1
5:  2     b 0 1 1
DT2 <- data.table::data.table(ID=c("1","1","1","2","2"),
                             CLASS=c("a","a","b","c","b"),
                             a=c(0,0,0,0,0),
                             b=c(0,0,0,0,0),
                             c=c(0,0,0,0,0), 
                             RXDate = c("1-1-99", "1-2-99", "1-3-99", "5-4-00", "6-5-01"))

# 2nd row from the data provided has different RXDate under same ID and Class.
# Use x[1] to pick first
DT_dcast <- dcast(DT2[, .(ID, CLASS, RXDate)], ID ~ CLASS, 
                  fun.aggregate = function(x) x[1], 
                  value.var = c("CLASS", "RXDate"))
DT_dcast
   ID CLASS.1_a CLASS.1_b CLASS.1_c RXDate_a RXDate_b RXDate_c
1:  1         a         b      <NA>   1-1-99   1-3-99     <NA>
2:  2      <NA>         b         c     <NA>   6-5-01   5-4-00

# Convert 1 or 0 under CLASS
class_cols <- names(DT_dcast)[grepl("CLASS", names(DT_dcast))]
for (col in class_cols) set(DT_dcast, j = col, value = ifelse(is.na(DT_dcast[[col]]), 0, 1))

DT_dcast
ID CLASS.1_a CLASS.1_b CLASS.1_c RXDate_a RXDate_b RXDate_c
1:  1         1         1         0   1-1-99   1-3-99     <NA>
2:  2         0         1         1     <NA>   6-5-01   5-4-00

# Then merge with the original data.table
DT_m <- merge(DT2[, .(ID, CLASS, RXDate)], DT_dcast, by = "ID")
DT_m

   ID CLASS RXDate CLASS.1_a CLASS.1_b CLASS.1_c RXDate_a RXDate_b RXDate_c
1:  1     a 1-1-99         1         1         0   1-1-99   1-3-99     <NA>
2:  1     a 1-2-99         1         1         0   1-1-99   1-3-99     <NA>
3:  1     b 1-3-99         1         1         0   1-1-99   1-3-99     <NA>
4:  2     c 5-4-00         0         1         1     <NA>   6-5-01   5-4-00
5:  2     b 6-5-01         0         1         1     <NA>   6-5-01   5-4-00
Change values in a dataframe column with values in another column base on a condition

Change values in a dataframe column with values in another column base on a condition


By : User1m
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can use numpy.where to make the condition
code :
import numpy as np

origin['Latitude'] = np.where(origin['State'] == 'Arizona', substitute['Latitude_ex'], origin['Latitude'])
origin['Longitude'] = np.where(origin['State'] == 'Arizona', substitute['Longitude_ex'], origin['Longitude'])
shadow
Privacy Policy - Terms - Contact Us © voile276.org