首页 > 解决方案 > Pandas:TypeError:列表索引必须是整数或切片,而不是 DataFrame

问题描述

我正在处理指定文件夹中的大量输入 csv 文件。我正在使用 pandas 数据框来存储输入文件并打算做更多的数学运算。

import numpy as np
import matplotlib.pyplot as plt
import csv
import os
from scipy.interpolate import interp1d
import pandas as pd
import glob

user_info=[19,20,21,22,23] #number of charge states analyzed using fit.py 


path = r'C:\Users\my_folder' # use your path
all_files = glob.glob(path + "/*.csv")

data = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    data.append(df)

my_x=data[0].iloc[2:,0] #assuming that all the files have the same list of x for all data(1~200)

my_y=[]
for filenumber in range(len(all_files)):
    my_y.append(data[filenumber].iloc[2:,1:len(user_info)+1])

在此示例中,所有分析的文件都是两个 csv 文件。

In[469]: all_files
Out[469]: 
['C:\\Users\my_folder\my_csv_file_001.csv',
 'C:\\Users\my_folder\my_csv_file_002.csv']

“数据”是 2 个元素的列表。每个元素都包含来自每个 csv 文件的信息。每个元素都被读取为 DataFrame。

In [471]:data[0]
Out[471]: 
[      -2   8125     7350    6725
 0     -1   8550     7700    7025
 1      0      0       51       0
 2      1      0       20       0
 3      2      0       57       0
 4      3      0      196       0
 5      4      0       92       0
 6      5      0       91      62
 7      6      0       52      85
 8      7      0        0       0
 9      8      0      201       0
 10     9      0       36       0
 11    10      0      104      14
 12    11      0       96       0
 13    12     89        0       0
 14    13     20       94       0
 15    14    149      120       0
 16    15      0       54      41
 17    16     18        0      29
 18    17      0       61      59
 19    18    168      285    1375
 20    19    575    22737   94126
 21    20   5198   520564  364946
 22    21  18037  1649117  594484
 23    22  51229  2189268  198710
 24    23  45691  1242980   28893
 25    24  11040   207948    3771
 26    25   1843    27105    1077
 27    26    239     9262     863
 28    27    233     5533     357
 29    28     91     3159     120
 ..   ...    ...      ...     ...
 171  170      0      117      47
 172  171      0       48       0
 173  172      0      186       0
 174  173      0       58       0
 175  174      0       26       0
 176  175      0        0       0
 177  176      0        0       0
 178  177      0        0       0
 179  178      0        0      32
 180  179      0      103       0
 181  180      0       64       0
 182  181      0       39      82
 183  182      0       17       0
 184  183      0       45      58
 185  184      0        0       0
 186  185      0        0       0
 187  186      0       45       0
 188  187      0        0       0
 189  188      0       18       0
 190  189      0        0       0
 191  190      0       89       0
 192  191      0      139       0
 193  192      0       38      28
 194  193      0      110       0
 195  194      0      119       0
 196  195      0        0      83
 197  196      0       28     123
 198  197      0        0      56
 199  198      0       44       0
 200  199      0       84       0

 [201 rows x 4 columns]]

my_y 值是除第一列之外的所有列。

In [476]: my_y[0]
Out[476]: 
[      8125     7350    6725
 2        0       20       0
 3        0       57       0
 4        0      196       0
 5        0       92       0
 6        0       91      62
 7        0       52      85
 8        0        0       0
 9        0      201       0
 10       0       36       0
 11       0      104      14
 12       0       96       0
 13      89        0       0
 14      20       94       0
 15     149      120       0
 16       0       54      41
 17      18        0      29
 18       0       61      59
 19     168      285    1375
 20     575    22737   94126
 21    5198   520564  364946
 22   18037  1649117  594484
 23   51229  2189268  198710
 24   45691  1242980   28893
 25   11040   207948    3771
 26    1843    27105    1077
 27     239     9262     863
 28     233     5533     357
 29      91     3159     120
 30      29     1637       0
 31      64      403      48
 ..     ...      ...     ...
 171      0      117      47
 172      0       48       0
 173      0      186       0
 174      0       58       0
 175      0       26       0
 176      0        0       0
 177      0        0       0
 178      0        0       0
 179      0        0      32
 180      0      103       0
 181      0       64       0
 182      0       39      82
 183      0       17       0
 184      0       45      58
 185      0        0       0
 186      0        0       0
 187      0       45       0
 188      0        0       0
 189      0       18       0
 190      0        0       0
 191      0       89       0
 192      0      139       0
 193      0       38      28
 194      0      110       0
 195      0      119       0
 196      0        0      83
 197      0       28     123
 198      0        0      56
 199      0       44       0
 200      0       84       0

 [199 rows x 3 columns]]

我想提取每一列并做一些数学运算(np.cumsum)。但我不知道如何访问 my_y(list) 中 DataFrame 中的各个列。例如我想得到 y 的第一列:

0
0
0
0
0
0
0
0
0
0
0
89
20
149
0
18
0
168
575
5198
18037
51229
45691
11040
1843
239
233
91
29
64
0
51
0
0
0
0
0
0
0
38
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
33
0
0
0
0
0
0
18
0
0
0
38
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
32
0
0
0
0
0
0
0
0
0
0
0
0
13
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
18
0
0
0
0
0
0
51
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

但如上所示,my_y[0] 选择了第一个 DataFrame 中的所有 y 值。

有任何想法吗?

标签: pythonpython-3.xpandasdataframe

解决方案


您可以将所有 csv 连接到一个数据框中。操作起来会更容易。假设所有 csv 的列与您没有任何标题的位置相同。我看到您在选择 x 和 y 时跳过了第一行,您也可以skiprows使用pd.read_csv.

import pandas as pd
import glob

user_info=[19,20,21,22,23] #number of charge states analyzed using fit.py 


path = r'C:\Users\my_folder' # use your path
all_files = glob.glob(path + "/*.csv")

data = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0, skiprows=2)
    data.append(df)

dataframe = pd.concat(data, axis='columns') # this assumes that x are the same and you only add columns in the different csv
my_x = dataframe.iloc[:, 0]
my_y = dataframe.iloc[:, 1:]

推荐阅读