Python 数据帧添加迭代

Python 数据帧添加迭代,python,pandas,Python,Pandas,我有一个奇怪的(至少对我来说,因为我只是一个初学者)。无论如何,我有一个由excel文件制作的熊猫数据框 df = pd.read_excel(excel_file_path_from_db, engine='openpyxl', sheet_name='Sheet1', skiprows=1) 直截了当地说,它是有效的。然后,我进行一些数字运算,在excel文件中添加几列,并在本例中使用openpyxl进行更新。经过数字运算后,我使用openpyxl保存excel文件 wb.save(exc

我有一个奇怪的(至少对我来说,因为我只是一个初学者)。无论如何,我有一个由excel文件制作的熊猫数据框

df = pd.read_excel(excel_file_path_from_db, engine='openpyxl', sheet_name='Sheet1', skiprows=1)
直截了当地说,它是有效的。然后,我进行一些数字运算,在excel文件中添加几列,并在本例中使用openpyxl进行更新。经过数字运算后,我使用openpyxl保存excel文件

wb.save(excel_file_path_from_db)
所有更新的值都保存在文件中。很好,到目前为止进展顺利。现在我想从我输入到excel文件的最后12列中创建一个新的数据框。所以我通过再次读取文件来创建一个数据帧

df_from_updated_excel = pd.read_excel(excel_file_path_from_db, engine='openpyxl', sheet_name='Sheet1', skiprows=1)
现在,我选择最后12列作为我的新数据帧

 df_last_12 = df_from_updated_excel[:, -12:] 
然后我试着在我的df_last_12中打印“hello”列,我是这样做的

print(df_last_12['hello']) 
问题是在我的原始数据框中以前有一个“hello”列,我在新数据框中输入了一个新的hello列,所以我得到了hello.1 hello.2,当时我认为应该在我的数据框中得到“hello”。
有趣的是,如果我打印
df_last_12
,我所期望的是只会有一个“hello”列。但它似乎有这些奇怪的迭代。你知道我是如何设置的吗,这样我就不会得到这些hello的迭代了?

你的逻辑看起来是正确的。这一定是因为列名不是您期望的那样。excel.columns
df\u from\u updated\u返回什么?它必须包括hello.1和hello.2

输出
感谢您的回复,不知什么原因,当您从更新的excel.columns中获取df_时,我得到了多个Hello。df中有hello列,我在excel中又添加了一个标题为hello的列。但我将datadrame设置为最后12行,其中应该只有1个hello。这就是为什么我只希望在数据帧中有1个hello,而不是hello.2
df = pd.DataFrame(columns=list("ABCDEFGHIJKLMNONPQRSTUVWXYZ")+["hello"], index=[i for i in range(5)])
df["hello"] = df.index
df = df.iloc[:,-12:]
print(df, "\n", df["hello"])
     P    Q    R    S    T    U    V    W    X    Y    Z  hello
0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN      0
1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN      1
2  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN      2
3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN      3
4  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN      4 
 0    0
1    1
2    2
3    3
4    4
Name: hello, dtype: int64