Issue
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
Solution
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
Answered By - Andre Nevares
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.