Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

python - Curious( and seemingly unexplainable?) problem when replacing array of chars with pandas.where()

In preparations for machine learning tasks on EEG-data i am trying to learn Pandas, and i have struggled alot with some basic challenges.

Brief explaination of what i am trying to do: I have

time_series - dataframe with the eegdata for 64 electrodes(340 000*64)

time_stamps - dataframe with timestamps for the EEG-data(340 000*1)

marker_stamps - dataframe with timestamps for when stimuli was presented to the subject(30*1)

markers - dataframe with markers for what stimuli was presented(30*2)(char)

since stimuli was presented 3 seconds at a time, I wanted to make a mask that shows at what timestamps stimuli was on the screen(e.g. 340 000*2,char) which later could be merged with time_series and time_stamps for a compact dataframe with all needed information.

I realized that to do this i still had to loop through the marker_stamps(please correct me if know how I could do it without looping). So I made a dataframe(340 000*2) with ['n','n'](coined as "idle" in contrast to the different stimuli. Then I looped through the markers and use pandas.where() to mask with the correct stimuli markers. Which looks like this:

a = pd.DataFrame('n', index=range(desc['sample_count']), columns=['0', '1'])
for i in range(5,35):
    cond = ((time_stamps - marker_stamps.iat[i,0]) > 3.0) | ((time_stamps - marker_stamps.iat[i,0]) < 0.0)
    a = a.where(cond.iloc[:,0], [markers.iloc[i,0], markers.iloc[i,1]] , axis=0)
    # a = a.where(cond.iloc[:,0], markers.iloc[i,0:1] , axis=0) ### why does this not work?

So the problem is marked with a comment in the code above: why does the commented line not work, should they not give the excact same output?

Pictures of the results below(exported to csv->screenshot in VSCode). The correct result first, the wrong result last.

This is the result with the uncommented line

This is the result with the commented line, why does this happen??

So my questions are:

First and foremost: Why do i need to split and put together the array with markes for it to be added correctly?

Secondly: Is my overall method reasonable? could this be done in a vectorized manner?

question from:https://stackoverflow.com/questions/65921562/curious-and-seemingly-unexplainable-problem-when-replacing-array-of-chars-wit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...