有很多类似的问题。这里有两个:
Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'
AttributeError: 'Object has no attribute'
我想用excel文件列标题中的空格替换下划线,然后保存。代码如下:
import pandas as pd
ws = r'c:/users/jpilbeam/Reverse911_1a_English_.xlsx'
# data frame from excel file
df3 = pd.read_excel(ws, header=0)
#remove underscores
df2 = df3.columns.str.replace("_", " ")
## save to file
df2.to_excel(df2)
这是完整的错误:
Traceback (most recent call last):
File "\\pathto\Python Scripts\C19VaccinationTable.py", line 18, in <module>
df2.to_excel(df2)
AttributeError: 'Index' object has no attribute 'to_excel'
在调试时,我注意到脚本将成功打印 columns.str.replace()
函数。但是,它不会写入excel文件。
答案 0 :(得分:1)
$domain = 'test.ru';
$values = [
'WMONID' => 'nRiQPgfdbrq',
'EUOBGSPNSESSIONID'=>'pc1PrDd8VGeeVvONfQSptl7fIOxBla_tcfvJ6GqViXWeLBGojsP\u00211681227827\u00211177731283'
];
$cookieJar = \GuzzleHttp\Cookie\CookieJar::fromArray($values, $domain);
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://test.ru',
]);
$response = $client->get('/endpoint',[
'cookies' => $cookieJar
]
是一个索引对象 (pandas.Index
),因此当您替换 df3.columns
时,它返回一个索引对象。而是这样做:
_
要查看它是如何工作的,您可以跟踪每个步骤:
import pandas as pd
ws = r'c:/users/jpilbeam/Reverse911_1a_English_.xlsx'
# data frame from excel file
df3 = pd.read_excel(ws, header=0)
#remove underscores
df3.columns = df3.columns.str.replace("_", " ")
## save to file
df3.to_excel(filename) # filename being the file name you want to save it in.