Replace 1s with column names in a dataframe

rename column pandas
pandas replace
pandas set column names
pandas dataframe
pandas rename multiple columns
pandas rename column by index
add column name to existing dataframe pandas
pandas rename single column

You can do:

df$LOS <- c("me", "ls", "lg", "lo")[apply(df[, c("me", "ls", "lg", "lo")]==1, 1, which)]
# > df
#    hhid    pid pname    vid vname me ls lg lo   amt LOS
# 1  5668  93660    AB 462962  ABCD  1  0  0  0 20000  me
# 2  5595  93660    HG 608757  KJUI  1  0  0  0 20000  me
# 3  4724 100960    DC 772284  HIND  0  1  0  0 14000  ls
# 4  4756 100960    DC 772284  HIND  0  1  0  0 14000  ls
# 5  4856 100960    DC 772284  HIND  0  0  0  1 14000  lo
# 6  4730 100960    DC 293475  KAJA  0  1  0  0 14000  ls
# 7  4757 100960    DC 293475  KAJA  0  0  1  0 14000  lg
# 8  6320 100962    BA 656323  ADAR  1  0  0  0 27000  me
# 9  4758 100960    DC 293475  KAJA  0  1  0  0 14000  ls
# 10 6319 100962    BA 656323  ADAR  0  0  1  0 27000  lg
# 11 6311 100962    BA  81533  SANG  0  0  0  1 27000  lo
# 12 5477  93814    BH 465612  NARI  1  0  0  0 20000  me
# 13 6322 100962    BA 656323  ADAR  0  1  0  0 23000  ls

or (defining a vector of columnnames)

cols <- c("me", "ls", "lg", "lo")
df$LOS <- cols[apply(df[, cols]==1, 1, which)]

How to Replace Values in Pandas DataFrame, (2) Replace multiple values to a new value for an individual DataFrame column: df['column name'] = df['column name'].replace(['1st old value'� Rename multiple pandas dataframe column names. Commander Date Score; Cochice: Jason: 2012, 02, 08: 4: Pima: Molly: 2012, 02, 08: 24: Santa Cruz


You could convert the data into longer format and filter the rows with value = 1

library(dplyr)

tidyr::pivot_longer(df, cols = c(me, ls, lg, lo), names_to = "LOS") %>%
  filter(value == 1) %>%
  select(-value)

#   hhid    pid pname    vid vname   amt LOS 
#   <dbl>  <dbl> <fct>  <dbl> <fct> <dbl> <chr>
# 1  5668  93660 AB    462962 ABCD  20000 me   
# 2  5595  93660 HG    608757 KJUI  20000 me   
# 3  4724 100960 DC    772284 HIND  14000 ls   
# 4  4756 100960 DC    772284 HIND  14000 ls   
# 5  4856 100960 DC    772284 HIND  14000 lo   
# 6  4730 100960 DC    293475 KAJA  14000 ls   
# 7  4757 100960 DC    293475 KAJA  14000 lg   
# 8  6320 100962 BA    656323 ADAR  27000 me   
# 9  4758 100960 DC    293475 KAJA  14000 ls   
#10  6319 100962 BA    656323 ADAR  27000 lg   
#11  6311 100962 BA     81533 SANG  27000 lo   
#12  5477  93814 BH    465612 NARI  20000 me   
#13  6322 100962 BA    656323 ADAR  23000 ls   

pandas.DataFrame.replace — pandas 0.25.0 documentation, Note that column names (the top-level dictionary keys in a nested dictionary) cannot be df.replace([0, 1, 2, 3], 4) A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e. Change Column Names in DataFrame. DataFrame object has an Attribute columns that is basically an Index object and contains column Labels of Dataframe. We can get the ndarray of column names from this Index object i.e. # Get ndArray of all column names columnsNamesArr = dfObj.columns.values


Base R solution:

df <- transform(df[!(names(df) %in% c("me", "ls", "lg", "lo"))], 

                name = names(df)[(names(df) %in% c("me", "ls", "lg", "lo"))][apply(df[(names(df) %in% c("me", "ls", "lg", "lo"))], 1, which.max)])

Data:

df <- data.frame(

  hhid = c(5668,5595,4724,4756,4856,4730,4757,6320,4758,6319,6311,5477,6322),

  pid=c(93660,93660,100960,100960,100960,100960,100960,100962,100960,100962,100962,93814,100962),

  pname=c("AB","HG","DC","DC","DC","DC","DC","BA","DC","BA","BA","BH","BA"),

  vid=c(462962,608757,772284,772284,772284,293475,293475,656323,293475,656323,81533,465612,656323),

  vname=c("ABCD","KJUI","HIND","HIND","HIND","KAJA","KAJA","ADAR","KAJA","ADAR","SANG","NARI","ADAR"),

  me=c(1,1,0,0,0,0,0,1,0,0,0,1,0),

  ls=c(0,0,1,1,0,1,0,0,1,0,0,0,1),

  lg=c(0,0,0,0,0,0,1,0,0,1,0,0,0),

  lo=c(0,0,0,0,1,0,0,0,0,0,1,0,0),

  amt=c(20000,20000,14000,14000,14000,14000,14000,27000,14000,27000,27000,20000,23000))

pandas.DataFrame.rename — pandas 1.1.0 documentation, Can be either the axis name ('index', 'columns') or number (0, 1). The default is ' index'. copybool, default True. Also copy underlying data. inplacebool, default� Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. inplace bool, default False. If True, in place.


Python, One such feature is the use of Data Frames. They are rectangular grids representing columns and rows. While creating a Data frame, we decide� How to get column names in Pandas dataframe 05-12-2018 While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations.


Python, It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns. Each row is a measurement of some instance� I am trying to remove spaces from a dataframe I have. The columns names look like below. I am trying to get the spaces between name out and replace it with "_" wherever present. ['join_date' '


How To Change Column Names and Row Indexes in Pandas , How to rename columns of pandas dataframe? How to change row names or row indexes of a pandas dataframe? Let us first load pandas . 1. I have two data frames. I have a list of column names in a data frame NAME. I then have another data frame DF where the column name is corresponded to data frame NAME in the next column. I need to replace these names to the column names in DF.