我保存了一些由python代码生成的数据。它们使用命令以Excel格式(.csv
(保存
with open(path + '/data_Sevol.csv', 'w', newline='') as csvfile:
fieldnames = ['Sevol']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for i in range(len(Sevol)):
writer.writerow({'Sevol': Sevol[i]})
并且具有以下格式(100行×1列(:
[[1,2,3,4,5]]
[[6,7,8,9,1]]
[[2,3,4,5,6]]
[[7,8,9,1,2]]
在jupyter的另一个笔记本中,我称这个文件为
data_Sevol = pd.read_csv(path + str("\") + str("data_Sevol.csv"))
并且它被正确地读取。但我不知道如何从中调用特定的行或列。例如,当我这样做时,data_Sevol[0]
或data_Sevol[1]
,认为程序会返回文件的第0行或第1行,jupyter会打印以下错误:
KeyError Traceback (most recent call last)
Input In [27], in <cell line: 1>()
----> 1 data_robs[0]
File ~anaconda3libsite-packagespandascoreframe.py:3505, in DataFrame.__getitem__(self, key)
3503 if self.columns.nlevels > 1:
3504 return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
3506 if is_integer(indexer):
3507 indexer = [indexer]
File ~anaconda3libsite-packagespandascoreindexesbase.py:3623, in Index.get_loc(self, key, method, tolerance)
3621 return self._engine.get_loc(casted_key)
3622 except KeyError as err:
-> 3623 raise KeyError(key) from err
3624 except TypeError:
3625 # If we have a listlike key, _check_indexing_error will raise
3626 # InvalidIndexError. Otherwise we fall through and re-raise
3627 # the TypeError.
3628 self._check_indexing_error(key)
KeyError: 0
加载文件后,您可以打印前5行,并查看列名和行名:
data_Sevol.head()
之后,如果您想通过名称访问任何行或列,您可以使用以下代码:
data_Sevol.loc[row_name, col_name]
如果您想使用行号或列号访问它,请使用以下代码:
data_Sevol.iloc[row_num, col_num]
请参考此链接:https://www.shanelynn.ie/pandas-iloc-loc-select-rows-and-columns-dataframe/