Skip to content
Advertisement

Append Data to DataFrame

I did a code in which I pick P-wave seismic waveform, basically is a time pick along waveform, also compute the signal-to-noise and other variables.

Here is part of my code.

for tr in st:
    tr_cut = tr.trim(A, B) #cut the waveform from A to B section
#Here starts to compute the P time, Signal-to-noise, etc    
    scnl, picks, polarity, snr, uncert = picker.picks(tr_cut)
    all_ = []
for i in range(len(picks)):
    print (scnl, picks[i], polarity, snr, uncert) # this is the output
# Here I try to append the data inside a DataFrame
    all_.append(
        {
            'scnl': scnl,
            'picks': picks[i],
            'snr': snr,
            'polarity': polarity,
            'uncert': uncert
         }
     )
     pd.DataFrame(all_)

But I am not being able to append the output from the function I specify here

scnl, picks, polarity, snr, uncert = picker.picks(tr_cut)

However if I print the output of the function I get the results

print (scnl, picks[i], polarity, snr, uncert) gives an output like this:

BBOJ.SHZ.BO., UTCDateTime(2020, 11, 10, 13, 9, 59, 200000), '', 8.1999993, 0.02 
BBOD.SHZ.BO., UTCDateTime(2020, 11, 10, 13, 10, 4, 260000), 'D', 9.5, 0.02 
BBOB.SHZ.BO., UTCDateTime(2020, 11, 10, 13, 10, 7, 620000), 'C', 8.90004, 0.040000

The aim of my idea is to append all this output inside the DataFrame but I can not do it. Moreover while I try to convert my DataFrame to CSV I get the following error.

‘list’ object has no attribute ‘to_csv’

Would you mind give me feedback what I am doing wrong, thank you Tonino

Advertisement

Answer

In order to make it work I modified a little the code with @Deep suggestions.

all_ = []
for tr in st:
    tr_cut = tr.trim(A, B) #cut the waveform from A to B section
#Here starts to compute the P time, Signal-to-noise, etc    
    scnl, picks, polarity, snr, uncert = picker.picks(tr_cut)
   
for i in range(len(picks)):
    print (scnl, picks[i], polarity, snr, uncert) # this is the output
# Here I try to append the data inside a DataFrame
    all_.append(
        {
            'scnl': scnl,
            'picks': picks[i],
            'snr': snr,
            'polarity': polarity,
            'uncert': uncert
         }
     )
     df = pd.DataFrame.from_dict(all_, orient='columns')
     df.to_csv("Picks.csv")

Moving the variable all_ = [] outside of the first loop solved my issue

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement