• Register
Welcome to Kodlogs Q&A, programming questions and answer website.

Most popular tags

java program python php javascript android c# r mysql spring c spring-boot eclipse arrays ggplot2 python-3 exception tomcat sql-server numpy jdbc windows string html hibernate ssl sockets x keytool python-2 pandas sql 7 asp net java-8 intellij-idea ssl-certificate entity-framework macos minecraft ios csv facebook security json list class spring-mvc heap-memory reactjs for-loop scikit-learn winforms glmnet webpack illegalstateexception matlab redirect code program junit pip django maven docker ubuntu typeerror linux loops node math datetime js jquery cryptography loop int import https laravel-5 tsql unity3d fatal-error struts java-applet pytorch runtime-error conda visual-studio apache-spark pyspark garbage-collection amazon-web-services sql-server-2005 quirks-mode internet-explorer-9 internet-explorer reporting-services anova machine-learning keras indentation-error laravel unit-testing android-studio jupyter-notebook orm qt virtualenv webclient prediction visual-studio-2010 compiler-errors numpy-ndarray postgresql curl vagrant aspnet reporting dataframe nullpointerexception javafx tensorflow jsp jpa wordpress servlet indentation xcode scanner sum github deployment socketexception git certificate css util oracle cmd recursion search tcp syntax connection float httpwebrequest dictionary database main function number file html5 functional-dependencies mysql-error-1064 node-sass gulp-sass archlinux sass data-science spring-batch psycopg2 backpropagation texture2d async-await net-mvc-4 google-calendar pdo ruby-on-rails-4 ruby-on-rails dbmigrate wp-mail github-for-windows facebook-android-sdk jsx xcb microsoft-metro react-router spring-data-jpa softmax elastic-beanstalk google-sheets-api google-spreadsheet-api cnf minidump xuggler acumatica gitlab elasticsearch-5 elasticsearch app-transport-security ios10 ios9 cdn cloudflare wampserver plpgsql aptitude salt-stack sysv parent-child android-edittext textview visual-studio-2012 android-workmanager unique-index unity2d electron theano maven-compiler-plugin pickle assetbundle rstudio bar-chart python-tesseract tomcat6 primefaces solr easymock jvm-crash tomcat7 apache-httpclient-4 web-services woodstox discord jersey-2 prepared-statement resultset sencha-cmd sencha-touch-2 sencha-touch powermock fips apex x509certificate ibm-bpm websphere-7 file-permissions file-io accesscontrolexception grizzly atmosphere slick2d lwjgl informetica twitter-bootstrap-3 jax-rs resteasy spring-security-kerberos spring-security kerberos long-integer mapnik git-review vpython continuation homebrew xgboost android-asynctask stack-trace user-interface jaxws-maven-plugin maven-3 maven-2 browser-notsupportedexception google-maps-api-3 google-maps visual-studio-code truststore firebase-authentication aws-organizations php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys windows-services react-redux inputstream facebook-graph-api entity-framework-4 reportingservices-2005 mips linear-regression deep-learning block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx eclipseide javafx-2 php-not-recognized command-line phpmyadmin tinyurl classnotfoundexception atom-editor android-emulator android-sdk-tools ionic2 cordova foobar2k tcplistener net-2 net-4 ole-db-provider export-to-excel openrowset windows-10 vagrantfile jvm-arguments global-variables autopostback classformatexception eof upstart ipv4 graphviz pydot atom curses unityeditor pandoc tesseract xls oledb redux spotify cxf wamp nio stacked headless typescript hyperlink outlook jackson keystore applicationcontext pipeline iis jstl encryption perfect-square objective-c carthage xcode8 indexing standard-deviation apt sudo xampp connector apple mdf kubernetes destructor gettime arithmeticexception gmail mix probability ioexception heuristics milliseconds disk angular cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jsf jtextfield awt jpanel thymeleaf inputmismatchexception glassfish deque anaconda jupyter flask lvalue gradle servlets netbeans simulation ibm factorial javax apache arduino boot opengl virtualbox jvm margins 2147483647 mongodb cloud firebase plot plugins processor automation crash card repository installer pointers editor concatenation formatting debugging devices module testing color arraylist sequence nested date caching expected build response ip directory algorithms release collections print figure casting url expression validation integer microsoft sorting sort read logging types replace rest 4 email ajax exe excel message required text variable time size dll system files table runtime static random code web map http error version 0 2 my
0 votes
pandas to csv: I am dealing with dataframe in pandas which I would like to write to a CSV file, how to achieve this?
by (1.5k points)  
reshown by

1 Answer

0 votes

How we can save or write these videos so let's say we've loaded our data frame we've manipulated the data maybe updated it changed a few values here and there hired new columns remove rows things like that and now we wanted to save it may be to send it somebody else so to you know save it for later processing it's quite easy to do that with pandas all we need to do is to use the to CSV function as we're gonna see so let's import our did whole pandas as PDS as usual we've seen this raw data dictionary before I'll forward it from one of the previous videos from one of the previous Jupiter notebook the notebooks that we've created so let's create a dictionary and then create a data frame out of it and now we can save that data frame as a CSV and the function to do is this is a function inside the data frame now it's not coming from pandas directly it's not like PD dot reaches V as we saw before but now we have the data frame and we use a function inside the data frame module to do the saving if I run that it'll save it to slash home slash new audience like download slash data dot CSV do that and if I display the contents the data frame contents the one we've said it looks like this a few interesting things to discuss here the first one is by default the separator is a comma and we can by the way have a look at the parameters that it uses and is quite you know it's quite actually a few of them so the separators comma by default the n/a or presentation they're not available what you want to do with that what I want the header or not by default it's true this index equals index equals true is quite interesting we'll see what that means what else do we have the encoding is none by default you can actually use compression if you want and that's quite interesting to reduce the size the quote char I've not spoken about this this is the issue when you have you know if you are for example you your separators comma and then one of your values has comma within it by default if we don't enclose that into quotes then it'll be that comma will be read as a separator and it can lead to errors in reading or writing the files so we can surround that or enclose and close that into double quotes the line Terminator by default is /n and so on and so forth.

Several options so let's have a look at the stuff that we've just saved and if we read it so we've saved it to this file here if we read it if we read it DF we've seen this before PD lot read CSV save it and data frame in the variable called data frame 1 and if we display it then we notice we have an extra column here unnamed 0 and then a value the values are 0 1 2 3 4 & 5 like the index the reason for this is that as we saw here the index is true by default where is it yeah index and pandas by default saves sorry pandas by default save the index as a new column and we can disable that by saying index equals false if we run that you have a look at the data frame we read it again and display it now and you notice that column has gone I didn't have a column name or label partners why default gave it unnamed here but like we said before it's quite convenient sometimes to disable the index what else we can also change the separating character instead of comma we can say for example let's make it /t save and then when we read we're gonna have to if we want to specify that then it'll be like a mess but we need to say separated equals / the way and then our data frame will read nicely right what else can we talk about here I think I think that's that's good enough for this video and if anything comes up you know in the future we'll deal with it but generally this is how you read and write is V Files and this is how you change the separator turn the index into false and we spoke about using the first row as the header yes or no disable I'm sorry true or false disable or enable and always have a look at the documentation by doing shift-tab here on your jupiter notebook you can you know get it larger to more display space and you can read through the variables i'm sorry - through the parameters that you would like to use this so the compression for example is quite interesting you can say compression equals gzip or XZ or be easy-peasy - subdued wanted to reduce this file so as you can play about with a quote a quote char and so on and so forth . 

by (8.9k points)