简体   繁体   中英

Split Two Related DataFrame Columns into Two New DataFrames

I basically have 2 related columns in a data frame in python. One of the columns is binary ie 1,0,0,1,0 etc and the next column has a related value ie 200, 34, 124, etc. I want to take all the zero values with their corresponding values in the adjacent column and create a new data frame and do the same for all the ones. An illustration of the columns are below;

Location     Price
1             24
0             200
0             56
0             89
1             101
1             94
1             3

You can make two new dataframes with just ones and zeros like this, IIUC:

df[df.Location == 0]    
#   Location  Price
#1         0    200
#2         0     56
#3         0     89

df[df.Location == 1]                                                                                                                                                
#   Location  Price
#0         1     24
#4         1    101
#5         1     94
#6         1      3  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM