我有一个数据框列表。列表中的每个数据框都是唯一的 - 这意味着有一些共享但不同的列。我想创建一个包含数据框列表中所有列的单个数据框,如果元素不存在,将填充 NaN。我试过以下
import pandas as pd
df_new = pd.concat(list_of_dfs)
#I get the following: InvalidIndexError: Reindexing only valid with uniquely valued Index objects
问题似乎是由于列表中的数据框。每个数据框只有一行,因此它的索引为零,因此重新索引不会起作用。我试过这个:
list_of_dfs.append(pd.DataFrame([rows], columns = tags).set_index(np.array(random.randint(0,5000))))
几乎生成一个随机数作为索引。但是,O 收到此错误:
ValueError: The parameter "keys" may be a column key, one-dimensional array, or a list containing only valid column keys and one-dimensional arrays.