Manolo Dominguez Becerra
Manolo Dominguez Becerra

Reputation: 1365

TypeError: object of type 'int' has no len() when converting list of tuples to pandas data frame

I have a list of tuples like this

tmp
[('chr19', 50003781, '+', 5303232448),
 ('chr19', 58856544, '+', 5303232448),
 ('chr19', 58856544, '+', 5303232448),
 ('chr10', 52559169, '+', 12460988980),
...]

and I have been following many answers from StackOverflow to convert this into a pandas data frame. Basically I need this

col1    col2      col3     col4
chr19   50003781.  +     5303232448

When I tried to do that with for example this

df = pd.DataFrame(tmp, columns=["col1", "col2", "col3","col4])

I got this error

TypeError: object of type 'int' has no len()

Why?

Upvotes: 0

Views: 722

Answers (1)

user7864386
user7864386

Reputation:

That means, tmp is not a list of tuples, there is an integer in it.

For example,

tmp = [('chr19', 50003781, '+', 5303232448),
 ('chr19', 58856544, '+', 5303232448),
 ('chr19', 58856544, '+', 5303232448),
 ('chr10', 52559169, '+', 12460988980), 1]

will give the same error. See if

any(isinstance(t, int) for t in tmp)

is True or not. If True it means there is an integer value in tmp. In that case, you probably want to filter it out when passing tmp to the DataFrame constructor.

Upvotes: 2

Related Questions