logo
Tags down

shadow

How can I select discrete columns from data frame


By : Michael Bachmann
Date : July 31 2020, 12:00 PM
it helps some times you can create a list of strings (pythonically) and then use that to select the columns:
code :
columns_of_interest = ([f'WeatherHR{i}' for i in range(24)] + 
                       ['Peak Average'] + 
                       [f'HR{i}' for i in range(25)])
data[columns_of_interest]


Share : facebook icon twitter icon

Replacing select columns in a data frame with new columns of equal length


By : SweetPopcorn
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a data frame which I will call "abs.data" that contains 265 columns (variables). I have another data frame which I will call "corr.abs" that contains updated data on a subset of the columns in "abs.data". Both data frames have an equal number of rows, n=551. I need to replace the columns in "abs.data" with the correct observations in "corr.abs" where the column names match. I have tried the following , you can find the common column names using
code :
comm_col <- intersect(colnames(abs.samps), colnames(corr.abs))
 x<-subset(abs.samps, select = -X2)
 y<-cbind(corr.abs$X2,x)

R: how to select data from a data frame based on specific rules and add the data as new columns to an existing data fram


By : Rajat Mhetre
Date : March 29 2020, 07:55 AM
this one helps. I have two data frames df1 and df2. , if i am understanding your question correctly this should work:
code :
### we use the matches to pick our values from df1
### we use our conditions to pick our values from df2
matches <- match(df2$y1,df1$x1)
matches <- matches[!is.na(matches)]
condition1 <- df2$y1 %in% df1$x1
condition2 <- df2$y2[condition1] >= df1$x2[matches]
condition3 <- df2$y2[condition1] <= df1$x3[matches]

### i create these tmp variables so you can see step by step
### what each line of code is doing
### here i am finding the values that meet all the conditions
### then i am pulling the associated y2 values
tmp <- data.frame(x1=df1$x1[matches],y2=df2$y2[condition1])
tmp <- tmp[condition2&condition3,]
tmp <- droplevels(tmp)

### now that we have the values we want
### we are organizing the data in the desired output you 
### specified. 
x <- split(tmp[-1], tmp[[1]])
tmp2 <- data.frame()
for(i in 1:length(x)){

  df <- data.frame(t(unlist(x[[i]], use.names=FALSE)))
  colnames(df) <- seq(1,nrow(x[[i]]))
  tmp2 <- rbind.fill(tmp2,df)

}
colnames(tmp2) <- paste(rep("z",ncol(tmp2)),1:ncol(tmp2),sep="")
res <- data.frame(df1[df1$x1 %in% names(x),],tmp2)
res <- rbind.fill(res,df1[!df1$x1 %in% names(x),])

>res
   x1 x2 x3 z1 z2
1 A35  8 15  9 NA
2 A41 24 63 24 32
3 A49 33 54 NA NA

Select a consecutive range of data.frame columns using names of beginning and end columns


By : Mohamed Medhat
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to subset the columns of a data.frame using the interval of column names. , Use c() function, then it works when using column names
code :
> A <- data.frame(ID1=c(1,1),ID2=c(5,5),ID3=c(01901,01902))
> A
#   ID1 ID2  ID3
# 1   1   5 1901
# 2   1   5 1902

> b <- A[,c(2:3)]
> b
#   ID2  ID3
# 1   5 1901
# 2   5 1902

> b1 <- A[,c("ID2","ID3")]
> b1
#   ID2  ID3
# 1   5 1901
# 2   5 1902

> b2 <- A[,2:3]
> b2
#   ID2  ID3
# 1   5 1901
# 2   5 1902

Using data frame values to select columns of a different data frame


By : suhas harish
Date : March 29 2020, 07:55 AM
Hope that helps Using R it's important to keep your terminology precise. double_expression_combinations is not a dataframe but rather a matrix. It's easy to loop over columns in a matrix with apply. I'm a bit unclear about the exact test, but this might succeed:
code :
 apply( double_expression_combinations, 2,  # the 2 selects each column in turn
          function(cols){ sum( initial[ , cols[1] ] + initial[ , cols[2] ] == 2) } )

Select rows of the multi-level index data frame with index values equal to columns in another data frame in pandas


By : user1806699
Date : March 29 2020, 07:55 AM
Any of those help Use Index.isin with MultiIndex:
code :
df = df1[df1.index.isin(df2.set_index(['i1','i2']).index)]
df = df1[df1.index.isin(pd.MultiIndex.from_arrays([df2['i1'], df2['i2']]))]
df = df1.reset_index().merge(df2).set_index(['i1', 'i2'])
df = df2.join(df1, on=['i1', 'i2'], how='inner').set_index(['i1', 'i2'])
print (df)
       c
i1 i2   
a1 b1  1
a3 b3  3
Related Posts Related Posts :
  • Print a table with aligned columns in python
  • PYTHON AND IBPY: UNEXPECTED EOF WHILE PARSING ; I can't figure it out
  • Entity Linking with spacy/Wikipedia
  • Question about "running times" of a for loop
  • Best way to .clean and .strip long string?
  • Jupyter Labs: Kernel Dies when Converting Tuple to PandasData Frame
  • Writing a CSV table from column variables?
  • Calling a function using a dictionary - dictionary keys as its parameters, dictionary values as its arguments
  • if error then do something else - string split
  • Filling values in a column
  • What are these set operations, and why do they give different results?
  • How can I run a function forever?
  • For loop resulting in wrong output
  • How to vectorize this simple NumPy function?
  • Matplotlib: creating a scatter plot where each point is colored (weighted) based on its count of instances in the datase
  • other possible way to duplicate a string from one variable in python
  • Cannot delete the column in DataFrame Pandas by del function
  • How to convert a string into ASCII?
  • Assigning values to a new column in Pandas
  • Django 3 - Making Model's FK Dropdown Display Current User's Data Only
  • Python Error of unsupported operand types for *: 'Nonetype' and 'Float' with array problem
  • Python in Jupyter Notebooks: 'Table' is not defined
  • Extract ID and value from set of strings in an array
  • finding the position of an element within a numpy array in python
  • Cannot run apache airflow after fresh install, python import error
  • Replace climbing sequence with its average
  • How to create a new column with a conditional count in a groupby pandas dataFrame
  • In python how to mix two sentence?
  • returning the 2nd letter in a string in a list and sorted the words by it. keep getting indexing range out of bound. Pyt
  • Convert string in an a tuple into an integer
  • Comparison using modifiable signs (python)
  • Python Beginning in new line for each letter in string
  • Byte to integer conversion
  • Why is 1e100+1 == 1e100 if ints do not have a maximum?
  • How to show list of certain items from JSON
  • How to fill a circle contour at the edge of an image?
  • How to BEST extract information from multiple dataframes based on a series of if\else conditions and matching values? (G
  • Convert string to few variables with pattern
  • Python Deleting Multiple Lines
  • How to replace a string based on first and last character
  • How can I resolve problem with tag_add in Tkinter
  • Check if csv file is empty in pandas
  • pd.NA vs np.nan for pandas
  • How to not display () and '
  • How to write a large dask array (numpy.ndarray) to a Zarr file leveraging GPUs?
  • How to crop circle image from webcam OpenCV and remove background
  • Can we login on a website with pure Python urllib, modify some form values, and submit? (without Selenium browser automa
  • Python: Fastest way of packing a 2d array of binary values into UINT64 array
  • Using global variable from one python file to another
  • How to add "<>" symbols to a string?
  • "IndexError: list assignment index out of range" in python
  • counting of elements (strings) in list python
  • if a certain columns's var is lower then n i want to drop that column
  • ValueError: Units 'M' and 'Y' are no longer supported, as they do not represent unambiguous timedelta values durations
  • How I get data in one column and put into a list using pandas?
  • What takes more memory, an 8 char string or 8 digit int?
  • I want to know what is the purpose of lambda x=x in the below code?
  • Pandas groupby select top N rows based on column value AND group size share
  • Appending to list produces None
  • Multiply List of RGB values Python
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org