处理错误"TypeError:期望的元组,得到str".将CSV加载到 pandas 多级和多索引(pandas) [英] Handling error "TypeError: Expected tuple, got str" loading a CSV to pandas multilevel and multiindex (pandas)

查看:67
本文介绍了处理错误"TypeError:期望的元组,得到str".将CSV加载到 pandas 多级和多索引(pandas)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载CSV文件(此文件)以创建一个多索引e多级数据框.列中具有 5(五)个索引 3(三)级.

I'm trying to load a CSV file (this file) to create a multiindex e multilevel dataframe. It has 5(five) indexes and 3(three) levels in columns.

我该怎么办?这是代码:

How I can do? Here is the code:

df = pd.read_csv('./teste.csv'
                  ,index_col=[0,1,2,3,4]
                  ,header=[0,1,2,3]
                  ,skipinitialspace=True
                  ,tupleize_cols=True)

df.columns = pd.MultiIndex.from_tuples(df.columns)

预期输出:

variables                                                u                  \
level                                                    1                   
days                                                     1               2   
times                                                  00h 06h 12h 18h 00h   
wsid lat        lon        start               prcp_24                       
329  -43.969397 -19.883945 2007-03-18 10:00:00 72.0      0   0   0   0   0   
                           2007-03-20 10:00:00 104.4     0   0   0   0   0   
                           2007-10-18 23:00:00 92.8      0   0   0   0   0   
                           2007-12-21 00:00:00 60.4      0   0   0   0   0   
                           2008-01-19 18:00:00 53.0      0   0   0   0   0   
                           2008-04-05 01:00:00 80.8      0   0   0   0   0   
                           2008-10-31 17:00:00 101.8     0   0   0   0   0   
                           2008-11-01 04:00:00 82.0      0   0   0   0   0   
                           2008-12-29 00:00:00 57.8      0   0   0   0   0   
                           2009-03-28 10:00:00 72.4      0   0   0   0   0   
                           2009-10-07 02:00:00 57.8      0   0   0   0   0   
                           2009-10-08 00:00:00 83.8      0   0   0   0   0   
                           2009-11-28 16:00:00 84.4      0   0   0   0   0   
                           2009-12-18 04:00:00 51.8      0   0   0   0   0   
                           2009-12-28 00:00:00 96.4      0   0   0   0   0   
                           2010-01-06 05:00:00 74.2      0   0   0   0   0   
                           2011-12-18 00:00:00 113.6     0   0   0   0   0   
                           2011-12-19 00:00:00 90.6      0   0   0   0   0   
                           2012-11-15 07:00:00 85.8      0   0   0   0   0   
                           2013-10-17 00:00:00 52.4      0   0   0   0   0   
                           2014-04-01 22:00:00 72.0      0   0   0   0   0   
                           2014-10-20 06:00:00 56.6      0   0   0   0   0   
                           2014-12-13 09:00:00 104.4     0   0   0   0   0   
                           2015-02-09 00:00:00 62.0      0   0   0   0   0   
                           2015-02-16 19:00:00 56.8      0   0   0   0   0   
                           2015-05-06 17:00:00 50.8      0   0   0   0   0   
                           2016-02-26 00:00:00 52.2      0   0   0   0   0   

我需要处理错误"TypeError:预期的元组,得到str":

I need handling error "TypeError: Expected tuple, got str":

TypeError: Expected tuple, got str

推荐答案

由于部分列不是元组,它们是从索引 2368 2959 df.columns 中.
列为字符串的索引:

You are getting an error because some of your columns are not tuples, they are strings from index 2368 to 2959 in df.columns.
Indices where the columns are strings:

df.columns[2368:2959]
Index(['('z', '1', '1', '00h').1', '('z', '1', '1', '06h').1',
       '('z', '1', '1', '12h').1', '('z', '1', '1', '18h').1',
       '('z', '1', '2', '00h').1', '('z', '1', '2', '06h').1',
       '('z', '1', '2', '12h').1', '('z', '1', '2', '18h').1',
       '('z', '1', '3', '00h').1', '('z', '1', '3', '06h').1',
       ...
       '('z', '1000', '2', '06h').1', '('z', '1000', '2', '12h').1',
       '('z', '1000', '2', '18h').1', '('z', '1000', '3', '00h').1',
       '('z', '1000', '3', '06h').1', '('z', '1000', '3', '12h').1',
       '('z', '1000', '3', '18h').1', '('z', '1000', '4', '00h').1',
       '('z', '1000', '4', '06h').1', '('z', '1000', '4', '12h').1'],
      dtype='object', length=591)

由于您想要使用元组的多索引列数据帧,因此我们首先通过使用 re.findall regex pattern ='(\(.*?\)).,然后将此值通过 ast.literal_eval 传递,以自动将字符串转换为元组.最后,将 pd.MultiIndex.from_tuples 用作:

Since you want multi-index column dataframe using the tuples, so we are cleaning these strings first by taking the substring which is necessary using re.findall with regex pattern = '(\(.*?\)).' then passing this value through ast.literal_eval for converting string to tuple automatically. Finally, using the pd.MultiIndex.from_tuples as:

df = pd.read_csv('teste.csv',index_col=[0,1,2,3,4],header=[0,1,2,3],parse_dates=True)

import re
import ast

column_list = []
for column in df.columns:
    if isinstance(column,str):
        column_list.append(ast.literal_eval(re.findall('(\(.*?\)).',column)[0]))
    else:
        column_list.append(column)


df.columns = pd.MultiIndex.from_tuples(column_list,
                                       names=('variables', 'level','days','times'))


print(df.iloc[:,:6].head())
variables                                                u                    
level                                                    1                    
days                                                     1               2    
times                                                  00h 06h 12h 18h 00h 06h
wsid lat        lon        start               prcp_24                        
329  -43.969397 -19.883945 2007-03-18 10:00:00 72.0      0   0   0   0   0   0
                           2007-03-20 10:00:00 104.4     0   0   0   0   0   0
                           2007-10-18 23:00:00 92.8      0   0   0   0   0   0
                           2007-12-21 00:00:00 60.4      0   0   0   0   0   0
                           2008-01-19 18:00:00 53.0      0   0   0   0   0   0

这篇关于处理错误"TypeError:期望的元组,得到str".将CSV加载到 pandas 多级和多索引(pandas)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆