0

Here, i provide an example dataset which include the information of date time and the location.

p1=pd.DataFrame([2015,2017,2019], columns=['year'])
p2=pd.DataFrame([6, 9, 10], columns=['month'])
p3=pd.DataFrame([1, 7, 18], columns=['day'])
p4=pd.DataFrame([21, 5, 13], columns=['hpour'])
p5=pd.DataFrame([33, 8, 59], columns=['minute'])
p6=pd.DataFrame([7.16, 38.45, 31.40], columns=['second'])
p7=pd.DataFrame([56.7791, 33.214, 35.458], columns=['lat'])
p8=pd.DataFrame([70.5112, 69.2054, 73.6415], columns=['long'])
p9=pd.DataFrame([15.24, 8.64, 23.17], columns=['dep'])
p10=pd.DataFrame([5.4, 3.0, 6.1], columns=['mag'])
p11=pd.DataFrame([22122,22123, 22124], columns=['id'])
df=pd.concat([p1, p2, p3, p4, p5, p6,p7, p8, p9, p10, p11], axis=1)

I require to write the output in way like

2015   06 01 21330716056S7791   70E5112   1524 54              022122

explanation of the above format:

year(4 space) 2-digit-month (1 space) 2-digit-day (1 space) 2-digit-hour 2-digit-minute 2-digit-second(integer part)3-digit-second(float part) 3-digit-lat (integer part) S 4-digit-lat(float part) (3-space) 2-digit-longitude(integer part) E 4-digit_longitude(float part) 3-space 4-digit-depth (1 space) 2-digit magnitude (15-space) 6-digit-event id

Here is what i did so far

File=str(p1)+"   "+str(p2)+" "+str(p3) +" "+str(p4)+str(p5))+str(int(p6))+str(float(p6))+ str(int(p7)) "S" +str(float(p7))+ "   " +str(int(p8))+"E"+str(float(p8))+ "   "+str(int(p9))+str(float(p9))+ " "+str(int(p10))+str(float(p10))+"   
            "+str(p11)

However, i did not find this as a robust approach for file formatting.

Expected format style:

enter image description here

May someone share how i can structure this.

danial
  • 1
  • 1

1 Answers1

0

The easiest way is to first convert to string form and then call the string module method provided by pandas to stitch the content together, you can refer to the first few columns of content to modify their own subsequent code, the example is as follows

import pandas as pd

p1 = pd.DataFrame([2015, 2017, 2019], columns=['year'])
p2 = pd.DataFrame([6, 9, 10], columns=['month'])
p3 = pd.DataFrame([1, 7, 18], columns=['day'])
p4 = pd.DataFrame([21, 5, 13], columns=['hpour'])
p5 = pd.DataFrame([33, 8, 59], columns=['minute'])
p6 = pd.DataFrame([7.16, 38.45, 31.40], columns=['second'])
p7 = pd.DataFrame([56.7791, 33.214, 35.458], columns=['lat'])
p8 = pd.DataFrame([70.5112, 69.2054, 73.6415], columns=['long'])
p9 = pd.DataFrame([15.24, 8.64, 23.17], columns=['dep'])
p10 = pd.DataFrame([5.4, 3.0, 6.1], columns=['mag'])
p11 = pd.DataFrame([22122, 22123, 22124], columns=['id'])
df = pd.concat([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11], axis=1)
print(df)

df["month"] = df["month"].astype(str).str.rjust(2, "0")
df["day"] = df["day"].astype(str).str.rjust(2, "0")
print()
df["id1"] = df["hpour"].astype(str).str.rjust(2, "0") + df["minute"].astype(str).str.rjust(2, "0") + \
            df["second"].astype(str).str.replace(".", "").str.rjust(4, "0") + \
            df["lat"].astype(str).str.replace(".", "S").str.rjust(7, "0")
.......

print(df[["year", "month", "day", "id1", "long", "dep", "mag", "id"]])

enter image description here

maya
  • 1,029
  • 1
  • 2
  • 7
  • thanks for suggestions, in this case i need to be very careful in term of spaces. Is there any other way we can adopt to write each column at different position as per the information of line number. E.g year line 0:4, event id line 50:55 and so on. – danial Jun 14 '23 at 03:17