Try to search your question here, if you can't find : Ask Any Question Now ?

Nested lists to python dataframe

HomeCategory: stackoverflowNested lists to python dataframe
pragati asked 3 weeks ago

I have a nested numpy.ndarray of the following format (each of the sublists has the same size)

len(exp_data) # Timepoints
Out[205]: 42

len(exp_data[0])
Out[206]: 1

len(exp_data[0][0]) # Y_bins
Out[207]: 13

len(exp_data[0][0][0]) # X_bins
Out[208]: 43

type(exp_data[0][0][0][0])
Out[209]: numpy.float64

columns = ['Timepoint','Y_bin','X_bin','Values']

I want to move these into a pandas DataFrame such that each of the first 3 columns is numbered from 0 to N and ‘Value’ gets the float value.
I could do this with a series of loops, but that seems like a very non-efficient way of solving the problem.

In addition I would like to get rid of an nan values. Do I do this after creating the df or is there a way to skip adding them in the first place?

df = pd.DataFrame(columns = columns)
for t,timepoint in enumerate(exp_data):
    for y,y_bin in enumerate(timepoint[0]):
        for x,x_bin in enumerate(y_bin):
            df.loc[len(df)] = [t,y,x,x_bin]
df.dropna().reset_index(drop=True) 

The final format should be as follows (except I’d preferably like integers instead of floats, but not essential; int(t) etc. doesn’t do the trick)

df
Out[250]: 
     Timepoint  Y_bin  X_bin    Values
0          0.0    4.0   10.0 -1.179207
1          0.0    4.0   11.0 -1.374304
2          0.0    4.0   12.0 -1.769056
3          0.0    4.0   13.0 -1.917923
4          0.0    4.0   14.0 -1.996617
5          0.0    4.0   15.0 -2.434516
6          0.0    4.0   16.0 -2.894720
...
1 Answers
Best Answer
Jyoti answered 3 weeks ago
Your Answer

20 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com