Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 例如,如何对附加多个数据帧生成的数据帧重新编制索引。_Python_Pandas_Dataframe - Fatal编程技术网

Python 例如,如何对附加多个数据帧生成的数据帧重新编制索引。

Python 例如,如何对附加多个数据帧生成的数据帧重新编制索引。,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个dataframe,它是通过将多个dataframe附加到一个长列表中生成的。如图所示,默认索引是0到7之间的循环,因为每个原始df都有这个索引。总行数为240。因此,如何将新的df重新索引为0~239,而不是30 x 0~7 我尝试了df.reset\u index(drop=True),但似乎不起作用。我还尝试了:df.reindex(np.arange(240)),但它返回了错误 ValueError: cannot reindex from a duplicate axis 您

我有一个dataframe,它是通过将多个dataframe附加到一个长列表中生成的。如图所示,默认索引是0到7之间的循环,因为每个原始df都有这个索引。总行数为240。因此,如何将新的df重新索引为0~239,而不是30 x 0~7

我尝试了
df.reset\u index(drop=True)
,但似乎不起作用。我还尝试了:
df.reindex(np.arange(240))
,但它返回了错误

ValueError: cannot reindex from a duplicate axis

您似乎忘记了分配输出,因为默认情况下,
就地不起作用

df = df.reset_index(drop=True)
或:

但更好的解决方案是(如果使用)添加参数
ignore\u index=True

df = pd.concat([df1, df2, ..., df7], ignore_index=True)

您似乎忘记了分配输出,因为默认情况下,
inplace

df = df.reset_index(drop=True)
或:

但更好的解决方案是(如果使用)添加参数
ignore\u index=True

df = pd.concat([df1, df2, ..., df7], ignore_index=True)

您可以将
append()
方法更改为忽略索引:

df1.append(df2, ignore_index=True)

您可以将
append()
方法更改为忽略索引:

df1.append(df2, ignore_index=True)