-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_two_files
30 lines (19 loc) · 1.04 KB
/
merge_two_files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/home/repos/public/Python/bin/python3.6
#
"""
Intention is to merge to files (or to tables) based on specified column
"""
import pandas as pd
from functools import reduce
devices = pd.read_csv('devices.txt') #single column file, each line is device name, and column name is 'device_name'
output = pd.read_csv('output.csv') #multiple column file, first column is filled device names, this columns name is 'device_name'
data_frames = [devices, output]
# BOTH joins below will produce similar outpur, but order will be different
# 1. will order using devices as primary reference (left)
# 2. will order using output as primary referance (right)
# 3. NOT FOUND values will be filled with N/A
final_df = reduce(lambda left, right: pd.merge(left, right, on='device_name', how='outer'), data_frames).fillna('N/A')
print(final_df)
print("-------------------------------------------------------------------------------")
final_df2 = reduce(lambda left, right: pd.merge(right, left, on='device_name', how='outer'), data_frames).fillna('N/A')
print(final_df2)