Search code examples
pythonlistreadfilereadlines

show "object" and "filter" of stars data without duplicating the "object"


I want to show "object" & "filter" of stars data without duplicating the "object" because it will be hard to read data line by line.

here is my data:

Object    HJD 24...     Filter  Magnitude
SU_Hor    55896.30476      B      14.877
SU_Hor    55896.27438     Ic      13.885
SU_Hor    55896.27349      B      14.809
SU_Hor    55896.27397      V      14.434
SU_Hor    55896.40882     Ic      14.033
SU_Hor    55896.40829      V      14.540
SU_Hor    55896.40770      B      14.941
SU_Hor    55896.34973     Ic      13.958
SU_Hor    55896.34943      V      14.494
SU_Hor    55896.34906      B      14.861
SU_Hor    55896.30542     Ic      13.912
SU_Hor    55896.30512      v      14.440
SU_Hor    55897.38547      V      14.536
SU_Hor    55897.28281      B      14.882
SU_Hor    55897.28317      V      14.428
SU_Hor    55897.28347     Ic      13.927
RZ_Lyr    27359.3030       V      10.630
RZ_Lyr    27684.4510       V      10.610
RZ_Lyr    27685.4780       V      10.580
RZ_Lyr    27701.3150       V      10.700
RZ Lyr    27934.4560       V      10.660
RZ Lyr    27955.4100       V      10.570
rzlyr     30604.2000       V      11.030
RZ_Lyr    55314.5695       B      12.047
RZ_Lyr    55314.5724       B      12.036
RZ_Lyr    55314.5900       B      12.042
RZ_Lyr    55314.6105       B      12.045
RZ_Lyr    55314.6163       B      12.027
RZ_Lyr    55342.3509       B      12.057
RZLyr     55342.3557       B      12.058
RZ_Lyr    55342.3606       B      12.052
RZ_Lyr    55342.3654       B      12.058

this is my code so far:

# show the name of the Object

f = open("C:/Users/Acer/PycharmProjects/task2_data.txt", "r")
print(f.readlines())

var = f.loc[:, ["Object", "Filter"]]
print(var)
f['Object'].to_frame().head()

f.close()

And this is my error

Traceback (most recent call last):
  File "C:\Users\Acer\PycharmProjects\readlines\main.py", line 6, in <module>
    var = f.loc[:, ["Object", "Filter"]]
AttributeError: '_io.TextIOWrapper' object has no attribute 'loc'

Process finished with exit code 1


Solution

  • If you want to read the file, you can use pd.read_csv function with custom separator:

    import pandas as pd
    
    # read the file using pd.read_csv (adjust the sep= accordingly)
    df = pd.read_csv("your_file.txt", sep=r"\s{2,}", engine="python")
    
    df = df.drop_duplicates("Object")[["Object", "Filter"]]
    print(df)
    

    Prints:

        Object Filter
    0   SU_Hor      B
    16  RZ_Lyr      V
    20  RZ Lyr      V
    22   rzlyr      V
    29   RZLyr      B