Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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_Excel_Pandas - Fatal编程技术网

Python 索引器:熊猫中的索引超出范围

Python 索引器:熊猫中的索引超出范围,python,excel,pandas,Python,Excel,Pandas,我是熊猫的初学者。我想用pandas将一列从excel文件提取到python数组。我想在整个列中运行一个循环。我想打印该列中的第一个数据。以下是我的代码: import pandas as pd xl_workbook = pd.ExcelFile("Summary.xlsx") # Load the excel workbook df = xl_workbook.parse("Sheet1") # Parse the sheet into a dataframe df['transacti

我是熊猫的初学者。我想用pandas将一列从excel文件提取到python数组。我想在整个列中运行一个循环。我想打印该列中的第一个数据。以下是我的代码:

import pandas as pd
xl_workbook = pd.ExcelFile("Summary.xlsx")  # Load the excel workbook
df = xl_workbook.parse("Sheet1")  # Parse the sheet into a dataframe
df['transactionqtr'] = pd.to_datetime(df['transactionqtr'],errors ='coerce')
df['commencementdate'] = pd.to_datetime(df['commencementdate'],errors ='coerce')
df=df.dropna()
df['transactionqtr'] = df['transactionqtr'].apply(lambdax:x.date().strftime('%m%d%y'))
df['commencementdate'] = df['commencementdate'].apply(lambda x:x.date().strftime('%m%d%y'))
arr1 = df['transactionqtr'][1][1]
arr2 = df['commencementdate'][1][1]
print(arr1)
print(arr2)
但它在这一行中显示IndexError:index out-bounds:

arr1 = df['transactionqtr'][1][1]
arr2 = df['commencementdate'][1][1]
我应该如何解决这个问题?另外,请为我提供更好的代码。

我认为您需要使用或:

对于第一个值,使用
[]
索引:

a = arr1[0]
我认为您需要使用或:

对于第一个值,使用
[]
索引:

a = arr1[0]

你能包括整个错误消息吗?你能包括整个错误消息吗?AttributeError:'str'对象没有属性'tolist',它显示了这个错误。什么是
打印(df.head())
df=df.dropna()
之后?我没有使用这个。检查第六行。有。是的,我使用了df=df.dropna(),但没有使用这个打印(df.head())AttributeError:“str”对象没有属性“tolist”,它显示了这个错误。在
df=df.dropna()
之后是什么。是的,我使用了df=df.dropna(),但没有使用这个打印(df.head())