There are different txt files on the left and right, as shown below.From these two data, I would like to extract only data with matching strings (such as aaa00) and combine the two data to produce output.
I would appreciate it if you could tell me this kind of code.Thank you for your cooperation.
Txt files for:
aaa00, 123.22, 42.11 bbb00, 1872
aba00, 163.22, 73.11 aaa00, 2001
acc01,298.11,63.28aba00,789
... caa01,983
...
Expected output:
aaa00, 123.22, 42.11, 2001
aba00, 163.22, 73.11,789
You just need to join the two data.The following is an example of Pandas using pandas.merge
.
>>import pandas as pd
>>>df1 = pd.DataFrame([
... ["aaa00", 123.22, 42.11]
... ["aba00", 163.22, 73.11]
... ["acc01", 298.11, 63.28]
... ])
>>>df2 = pd.DataFrame([
... ["bbb00", 1872] ,
... ["aaa00", 2001] ,
... ["aba00", 789]
... ["caa01", 983]
... ])
>>>df1.merge(df2, how="inner", on=0)
01_x21_y
0 aaa00 123.22 42.11 2001
1aba 00 163.22 73.11789
To read from a CSV file for DataFrame, use pandas.read_csv
.
With the original Python.
This is possible if the CSV format is simple.
(If it's a typical CSV, it's better to use a CSV module.)
Also, as I checked with colab, the description is a little older than the current Python.
importio
f01 = ' '
aaa00, 123.22, 42.11
aba00, 163.22, 73.11
acc01,298.11,63.28
'''
f02 = ' '
bbb00,1872
aaa00,2001
aba00,789
caa01,983
'''
withio.StringIO(f01.strip()) as fp1:# with open(fname1) as fp1:
dct = {ln.split(',')[0]: ln.strip() for ln in fp1}
withio.StringIO(f02.strip()) as fp2:# with open(fname2) as fp2:
lst = [', '.join([dct[k]]]+v) for ln in fp2
fork, *vin(ln.trip().split(',') ifkindct]
lst
# ['aaa00, 123.22, 42.11, 2001', 'aba00, 163.22, 73.11, 789']
© 2025 OneMinuteCode. All rights reserved.