0

I am a python beginner and I have a python DataFrame with 5 columns as shown below. I need to check if value in column a is 1 then stack the corresponding value in column d and e together. Likewise I would check if value in column a is 2 then stacking the corresponding value in column d and e and so on of value in a = 3.

   a    b    c         d           e
   1    2   0.57735 -0.456086      9
   1    3   0.57735 -0.456086      9
   3    1   0.57735 -0.456086      9
   1    2   0.57735 -0.456086      9
   2    1   0.57735 -0.456086      9

My initial approach looks like this,

data= pd.read_csv('data.csv',usecols=['a','b','c','d','e'])
dis_j= pd.DataFrame(data)

if dis_j['at1']==1:
     sub_1 = np.stack(dis_j['d'], dis_j['e'])

However, it does not work properly. Looking forward to your comments. Thanks!

ali
  • 61
  • 2
  • 9
  • What do you mean by stack? the values in d are floats, do you want to create a list like: [val_d, val_e]? – Bruno Mello Apr 01 '20 at 22:19
  • `pd.read_csv` returns a `DataFrame`. There's no need to pass `data` through `pd.DataFrame` again. Do check the variables that your code produces, at each step. – hpaulj Apr 01 '20 at 23:23
  • **does not work properly**, those are meaningless words here! Explicitly tell us what was wrong, either the error, or the wrong result. – hpaulj Apr 01 '20 at 23:24
  • Does this answer your question? [calculate sum of Nth column of numpy array entry grouped by the indices in first two columns?](https://stackoverflow.com/questions/63871381/calculate-sum-of-nth-column-of-numpy-array-entry-grouped-by-the-indices-in-first) – ClimateUnboxed Oct 05 '20 at 06:38

0 Answers0