Cannot export Pandas dataframe to specified file path in Python for csv and excel both - python-2.7

I have written a program which exports my Pandas DataFrame to a csv as well as an excel file. However, the problem I am facing is that, randomly, the export function to both the file formats does not work, resulting in me seeing an error stating "No such File Path or Directory".
My code is as follows:
frame3.to_csv('C:/Users/Downloads/ABC_test.csv',index=False)
writer = pd.ExcelWriter('C:/Users/Downloads/ABCD.xlsx', engine='openpyxl')
frame3.to_excel(writer, sheet_name='Sheet1')
writer.save()
The major issue is that this code works sometimes and sometimes it does not! Going by what others have posted here, I tried to add the output directory by the use of
pth1 = os.path.join(r'C:/Users/Downloads/FinalProgram/', output_filename)
frame3.to_csv(pth1)
Sadly, this has no effect on this stubborn error. Would appreciate any help / insights possible on the matter.

Forgot to update - I figured a way around this particular problem:
Simply set the working directory for the program to be the output directory (as depicted by the command below), before the calling the 'to_csv' function.
os.chdir('F:/Codelah/')
On a side note, this was an issue I primarily faced on Windows OS - Ubuntu worked like a charm and did not require this workaround!
Hope this helps

Related

Identical rmd files create different latex files when run on two computers

When my colleague and I run the same Rmd file on our respective computers they produce different .tex files. This is a problem, because the tex-file my computer produces doesn't compile. Apparently there is some invisible local setting that is different between our computers but what could it be? I updated all the Rpackages I use but to no avail.
The Rmd file starts with
output:
bookdown::pdf_document2:
keep_tex: yes
toc: false
And both of us compile it by simply hitting the knit-button in Rstudio.
Noticeable differences in the tex-files are:
extra linebreaks in different places
a line that is commented out in the rmd-file (<!-- blabla -->) appears in my tex-file, not in his, but some other out-commented lines appear in neither (as they should)
at the end of lines in tables there is a \strut inserted in my tex-file but not in his
Section heads read \hypertaget{blabla} in his file but not mine
For none of these difference I can find any place in the Rmd-file where any choice w.r.t to this is made - apparently some local settings file I am not aware of is used in the process??
Please let me know if you need more information.
EDIT: we found a partial answer and full solution, but I am still interested in what the underlying mechanism is. It turned out that I was using an older version of Rstudio. (It took me long to find that out because the check for updates tool in Rstudio kept telling me that I was using the newest version, but that is a separate issue.) Using the same version of Rstudio we get the same result.
The translation from Rmd to tex has multiple steps:
All the code chunks are extracted and executed via knitr, resulting in a md file.
The md file is translated to tex via pandoc.
For most people pandoc comes bundled with RStudio. So when you updated that, you got a more recent pandoc version. You can test for the used pandoc version with rmarkdown::pandoc_version().

debugging options with rmarkdown

I am using the rmarkdown with the rshiny for generating word file reports. I am using the R studio-server for development. On executing the rshiny application, it halts due to some error in the one of the rmarkdown.
The error says...
Quitting from lines 11-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
Quitting from lines NA-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
It's providing the line numbers which are not useful to identify the root cause. Adding print statements are also not useful as I am generating the word file report, until and unless the complete .Rmd doesn't get successfully executed, I won't be able to see print statements output.
I tried changing the rmarkdown output setting from chunk output inline to chunk output to console as mentioned here as well but it is of no use.
Is there any way to print the .Rmd file print statements or the output to the console or is there any way to debug the .Rmd file?
In addition to my comment above, Abhinandan, I've recently stumbled across a new package, called testrmd.
Although it is new, it seems to work with a number of different test packages and provides a useful front-end for Rmarkdown documents. (I'm certainly going to use it.)
You might want to check it out. Here's the link: https://github.com/ropenscilabs/testrmd.
I hope this helps you.
See My .Rmd file becomes very lengthy. Is that possible split it and source() it's smaller portions from main .Rmd?
That's what I do. -
Split your code chunks in separate files and add them one by one

Stata do files in Atom (script package)

I am trying to run a do file in Atom (macOS). The script package detects the language correctly but is "unable to run".
Atom is started from the terminal, script works for other languages and Stata is on PATH.
Any idea what might be going wrong?
Just to note, the issue can also be solved on Windows by editing grammars.coffee as well, the default path being C:/Users/*username*/.atom/packages/script/lib/ and changing "stata" to the location of Stata on the machine. Highlighted text also doesn't work, as each line is executed in Stata with quotes around it, meaning Stata will interpret it as a single command name, not as a command with arguments.
Figured it out: .
The code of the script package needs to be modified. In the grammars.coffee file, I replaced both commands of Stata by "/Applications/Stata/StataSE.app/Contents/MacOS/StataSE". Works fine now.
Adding Stata to the path variable is not sufficient (might not be necessary even).
This works for me too — but I use StataMP so updated the filepath as such to /Applications/Stata/StataMP.app/Contents/MacOS/StataMP and things are working fine now.
That said, I'm not sure if script supports this, but when executing a highlighted selection of code in a .do file crashes my StataMP 14.2

'line contains NULL byte' error while using pyAudioAnalysis

I'm trying to run a regression on some audio data using pyAudioAnalysis, going off of the instructions here. However, when I run aT.featureAndTrainRegression(args), I get Error: line contains NULL byte as it scans through my csv file.
I've done some digging online, and there's a lot of advice on how to delete or ignore null bytes from your csv, but I didn't have a lot of luck with those approaches; going through and deleting all the null bytes left me with a csv of pure gobbledygook that had no resemblance to the original, and I'm hesitant to try the other side: modifying the audioTrainTest code in pyAudioAnalysis. I mean, enough people use this that I can't believe everyone encounters the error, so there must be a way to solve it without tinkering with the guts of the library.
I'm running a mac with OS10.9, and using Python 2.7 on PyCharm. Any advice would be greatly appreciated. Thanks in advance!
Figured it out! Turns out the csv filetype I was using (the mac one) wasn't compatible with python, hence the errors. When I saved my csv as a Windows csv all the problems disappeared.

cPickle.load() doesnt accept non-.gz files, what can I use for .pkl files?

I am trying to run an example of a LSTM recurrent neural network that is presented in this git: https://github.com/mesnilgr/is13.
I've installed theano and everything and when I got to the point of running the code, I've noticed the data was not being downloaded, so I've opened an issue on the github (https://github.com/mesnilgr/is13/issues/12) and this guy came up with a solution that consisted in:
1-get the data from the dropbox link he provides.
2- change the code of the 'load.py' file to download, and read the data properly.
The only issue is that the data in the dropbox folder(https://www.dropbox.com/s/3lxl9jsbw0j7h8a/atis.pkl?dl=0) is not a compacted .gz file as, I suppose, was the data from the original repository. So I dont have enough skill to change the code in order to do with the uncompressed data exaclty what it would do with the compressed one. Can someone help me?
The modification suggested and the changes I've done are described on the issue I've opened on the git(https://github.com/mesnilgr/is13/issues/12).
It looks like your code is using
gzip.open(...)
But if the file is not gzipped then you probably just need to remove the gzip. prefix and use
open(...)