python - How can I loop over only specific columns from a text file using pandas? -
i want loop:
for col in result.columns: result[col] = result[col].str.strip("{} ")
for columns "1h.l" , "1h_2.l" because other columns aren't strings.
my code is:
import pandas pd result = {} text = 'fe' filename = 'fe_yellow.xpk' if text == 'ee': df = pd.read_csv('peaks_ee.xpk', sep=" ",skiprows=5) shift1= df["1h.p"] shift2= df["1h_2.p"] if filename == 'ee_pinkh1.xpk': mask = ((shift1>5.1) & (shift1<6)) & ((shift2>7) & (shift2<8.25)) elif filename == 'ee_pinkh2.xpk': mask = ((shift1>3.25)&(shift1<5))&((shift2>7)&(shift2<8.5)) result = df[mask] result = result[["1h.l","1h.p","1h_2.l","1h_2.p"]] col in result.columns: result[col] = result[col].str.strip("{} ") result.drop_duplicates(keep='first', inplace=true) tclust_atom=open("tclust_ppm.txt","w+") result.to_string(tclust_atom, header=false)
the file reading in from:
label dataset sw sf 1h 1h_2 noesy_f1ef2e.nv 4807.69238281 4803.07373047 600.402832031 600.402832031 1h.l 1h.p 1h.w 1h.b 1h.e 1h.j 1h.u 1h_2.l 1h_2.p 1h_2.w 1h_2.b 1h_2.e 1h_2.j 1h_2.u vol int stat comment flag0 flag8 flag9 0 {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 1 {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 2 {1.h8} 8.13712 0.05000 0.10000 ++ {0.0} {} {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 3 {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {1.h8} 8.13712 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 4 {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} {2.h1'} 5.90291 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 5 {2.h1'} 5.90291 0.05000 0.10000 ++ {0.0} {} {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 6 {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 7 {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.h8} 8.13712 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 8 {1.h1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0 9 {1.h8} 8.13712 0.05000 0.10000 ++ {0.0} {} {2.h8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
i want output
1.h1' 5.82020 0.3 2.h8 7.61004 0.3 1.h8 8.13712 0.3 2.h1' 5.90291 0.3
the first column comes columns "1h.l" , "1h_2.l" second "1h.p" , "1h_2.p" , third column want want write every row. how can this?
you can pass list of columns names i.e
result = pd.dataframe({"1h.l":['{nice}','{so}'],"1h_2.l":['{nice}','{so}'],"2h.l":['nice','so']}) col in ['1h.l','1h_2.l']: result[col] = result[col].str.strip("{} ")
output :
1h.l 1h_2.l 2h.l 0 nice nice nice 1
Comments
Post a Comment