Jan
Jan

Reputation: 88

Python pandas tolist() not returning a list but [[]]

Why is the tolist() method not returning a list in my case, but something looking like the following?

Loaded metadata for ShotID: 007_0030 [['007_0030', '01.02.2018', '1001-1085', '28 mm', 'T. 4', '10 m', 'Super 35', 'Sony FS7', 'Zeiss Ultra Prime', '30', '172.8°', 'dolly', '', '', '', '']]

def loadshotinfo(self, df, shotid):
    ix = df['Shot#'].loc[df['Shot#'].str.contains(shotid)].index
    metadatalist = df.values[ix].tolist()
    print('Loaded metadata for ShotID: ' + shotid)
    print(metadatalist)

I thought that values returns an array and tolist() makes a list out of it, so why does the console put out something like [['lsdjasd','gjasvdj']] when a list should look like ['..','..']?


Solved: tolist() actually returns a list of lists.

Upvotes: 0

Views: 833

Answers (1)

DYZ
DYZ

Reputation: 57105

A DataFrame is a table. .tolist() returns a list of lists, one list per row. If you have only one row, extract it with df.values[ix].tolist()[0].

Upvotes: 1

Related Questions