gpt4 book ai didi

python - read_csv 多索引数据帧

转载 作者:行者123 更新时间:2023-12-01 02:50:37 25 4
gpt4 key购买 nike

我有以下 csv:

               value    value   value   value ...
id 1 1 1 2
indic 1 2 3 1
valuedate
05/01/1970 1.0 2.0 3.2 5.2
06/01/1970 4.1 ...
07/01/1970
08/01/1970

我想在 pandas DataFrame 中读取,所以我执行以下操作:

df=pd.read_csv("mycsv.csv", skipinitialspace=True, tupleize_cols=True)

但出现以下错误:

IndexError: Too many levels: Index has only 1 level, not 2

我怀疑多重索引可能存在错误,但我不明白如何使用 read_csv 的参数来解决此问题。(注意:valuedate 是索引列的名称)

我想将此数据放入一个多索引的 DataFrame 中:id 列下有几个印度语子列。

最佳答案

文件.csv:

               value    value   value   value
id 1 1 1 2
indic 1 2 3 1
valuedate
05/01/1970 1.0 2.0 3.2 5.2

做:

import pandas as pd

df = pd.read_csv("file.csv", index_cols=0, delim_whitespace=True)
print(df)

输出:

            value  value.1  value.2  value.3
id 1.0 1.0 1.0 2.0
indic 1.0 2.0 3.0 1.0
valuedate NaN NaN NaN NaN
05/01/1970 1.0 2.0 3.2 5.2

关于python - read_csv 多索引数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44833452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com