Leaving out the lead-up and data to the following because it looks like an error in how the list comprehension is written. It is meant to cycle through a list l
of pandas DataFrame
s and Series
, and label them based on whether they are two dimensional (both index and columns) or one dimensional (index only). Why the error (even if a close the line-break)?
[pd.DataFrame(A, index=labels, columns=labels) for A in l
if type(A) is pd.DataFrame else pd.Series(A, index=labels)]
results in
if type(A) is pd.DataFrame else pd.Series(A, index=tickers)]
^
SyntaxError: invalid syntax
You need to move the for A in l
to the end of your statement.
[pd.DataFrame(A, index=labels, columns=labels) if type(A) is pd.DataFrame else pd.Series(A, index=labels) for A in l]
Is you issue is not due to the is. Maybe try with isinstance(type(A), pd.DataFrame) Another point is the order of your list compréhension if think it should be
[f(x) if condition else g(x) for x in sequence]
So you can try
[pd.DataFrame(A, index=labels, columns=labels) if isinstance(type(A) , pd.DataFrame) else pd.Series(A, index=labels) for A in l ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.