0

a:

   Length  10kN
0      0.0     5
1      0.5     5
2      1.0     5
3      1.5     5
4      2.0     5
5      2.5     5
6      3.0     5
7      3.5     5
8      4.0     5
9      4.5     5
10     5.0     5
11     5.0    -5
12     5.5    -5
13     6.0    -5
14     6.5    -5
15     7.0    -5
16     7.5    -5
17     8.0    -5
18     8.5    -5
19     9.0    -5
20     9.5    -5
21    10.0    -5

b:

    Length1  20kN
0       0.0    50
1       0.5    45
2       1.0    40
3       1.5    35
4       2.0    30
5       2.5    25
6       3.0    20
7       3.5    15
8       4.0    10
9       4.5     5
10      5.0     0
11      5.5    -5
12      6.0   -10
13      6.5   -15
14      7.0   -20
15      7.5   -25
16      8.0   -30
17      8.5   -35
18      9.0   -40
19      9.5   -45
20     10.0   -50

c as a result of my code below:

    Length  10kN  Length1  20kN  Total
0      0.0     5      0.0  50.0   55.0
1      0.5     5      0.5  45.0   50.0
2      1.0     5      1.0  40.0   45.0
3      1.5     5      1.5  35.0   40.0
4      2.0     5      2.0  30.0   35.0
5      2.5     5      2.5  25.0   30.0
6      3.0     5      3.0  20.0   25.0
7      3.5     5      3.5  15.0   20.0
8      4.0     5      4.0  10.0   15.0
9      4.5     5      4.5   5.0   10.0
10     5.0     5      5.0   0.0    5.0
11     5.0    -5      5.5  -5.0  -10.0
12     5.5    -5      6.0 -10.0  -15.0
13     6.0    -5      6.5 -15.0  -20.0
14     6.5    -5      7.0 -20.0  -25.0
15     7.0    -5      7.5 -25.0  -30.0
16     7.5    -5      8.0 -30.0  -35.0
17     8.0    -5      8.5 -35.0  -40.0
18     8.5    -5      9.0 -40.0  -45.0
19     9.0    -5      9.5 -45.0  -50.0
20     9.5    -5     10.0 -50.0  -55.0
21    10.0    -5      NaN   NaN    NaN

Code I tried:

import pandas as pd
a=pd.read_csv("first.csv")
b=pd.read_csv("second.csv")
c=pd.concat([a,b], axis=1)
c['Total']=c['10kN']+c['20kN']
print(c['Total'])
print(a)
print(b)
print(c)

I want add two column 10kN and 20kN for same length

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
PYWIN
  • 1
  • 1
    Show me how you want the final dataframe to look like. Also, post the contents of your original csv as text (i.e. copy-paste from notepad, not copy-paste from excel). – Joshua Jul 02 '21 at 01:27
  • 1
    @Joshua Copy-paste from anywhere is fine as long as it is formatted *here* correctly. – Mustafa Aydın Jul 02 '21 at 06:55
  • @jezrael Is duplicate correct? I don't see a summation in the linked question, i.e., how to generate the `Total` column with those answers? – Mustafa Aydın Jul 02 '21 at 07:02
  • @MustafaAydın - If add new column then OP solution `c['Total']=c['10kN']+c['20kN']` working perfectly. – jezrael Jul 02 '21 at 07:02
  • @MustafaAydın - Or something missing? Can you explain more? – jezrael Jul 02 '21 at 07:03
  • @jezrael If it worked perfectly, they wouldn't ask the question, though... They need to provide the expected output as @Joshua said. Maybe they need a `.dropna` at the end... Or they want to sum only the matching indices between the two but we don't know (yet)... – Mustafa Aydın Jul 02 '21 at 07:04
  • Ya, I see problem there is incorrect aligned column, because joined by index, not by `Length`s. So if used solution from dupe, maybe here `map` should be good (Or merge) then output should be correct. But not sure if need `merge` with left join (missing values) or inner join (no missing value) – jezrael Jul 02 '21 at 07:06
  • @MustafaAydın - Agree, last ouput is not 100% clear, but first step is solution from dupe in my opinion. (or merge with inner join) – jezrael Jul 02 '21 at 07:07
  • 1
    @MustafaAydın - added new dupe for cover merge with inner join – jezrael Jul 02 '21 at 07:08
  • two datas have uneven column, as we can see index 10 from first and index 10,11 from second appear three times. – PYWIN Jul 02 '21 at 18:04
  • I want to add loads linked to that particular length – PYWIN Jul 02 '21 at 18:05
  • we can see correction required after 10th index in output – PYWIN Jul 02 '21 at 18:09
  • Length 10kN Length 10kN Total 0 5 0 50 55 0.5 5 0.5 45 50 1 5 1 40 45 1.5 5 1.5 35 40 2 5 2 30 35 2.5 5 2.5 25 30 3 5 3 20 25 3.5 5 3.5 15 20 4 5 4 10 15 4.5 5 4.5 5 10 5 5 5 0 5 5 -5 5 0 -5 5.5 -5 5.5 -5 -10 6 -5 6 -10 -15 6.5 -5 6.5 -15 -20 7 -5 7 -20 -25 7.5 -5 7.5 -25 -30 8 -5 8 -30 -35 8.5 -5 8.5 -35 -40 9 -5 9 -40 -45 9.5 -5 9.5 -45 -50 10 -5 10 -50 -55 I want result like this, which is created in excel – PYWIN Jul 02 '21 at 18:20

0 Answers0