Home > database >  How to add a row from a table to another table in Jupiter
How to add a row from a table to another table in Jupiter

Time:09-18

Suppose table A has 4 rows and 5 columns and table B has 2 rows and 5 columns

How can I select first row from table B and add it to table A?

I am using pandas

CodePudding user response:

Your data:

import pandas as pd
dict_1 = {'A': ['ww', 'qq', 'xx', 'tt', 'bb'],'B': [1, 71, 34, 2, 3],'C': [2, 87, 42,35,4],'D': [713,265,12,1,1],'E': [65,24 ,22 ,1 ,1]}
dict_2 = {'Counts': ['Female', 'Male', 'Total'],'A': [5,2,2],'B': [80,81,89],'C': [2,2,3],'D': [12,8,1],'E': [93,7,8]}

df_1 = pd.DataFrame(dict_1)
df_2 = pd.DataFrame(dict_2)

Insert a new Column on df_1:

df_1.insert(0, "Counts", '-')

Get the Last Line of DataFrame 02

line = df_2.iloc[-1:]

Append to the last line to the first dataframe

df_1 = df_1.append(line, ignore_index=True)

Output:

    Counts  A    B  C   D   E
0   -      ww    1  2   713 65
1   -      qq   71  87  265 24
2   -      xx   34  42  12  22
3   -      tt   2   35  1   1
4   -      bb   3   4   1   1
5   Total   2   89  3   1   8

CodePudding user response:

This is my main table:

    A   B   C   D   E
ww  1   65  2   713 65
qq  71  24  87  265 24
xx  34  117 42  12  11
tt  2   1   35  1   1
bb  3   1   4   1   1

I want to join last row from below table to above table:

Counts  A   B   C   D   E
0   Female  5   80  2   12  93
1   Male    2   81  2   8   7
2   Total   2   89  3   1   8
  • Related