Python Forum
export dataframe to file.txt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
export dataframe to file.txt
#2
(Apr-18-2022, 05:53 AM)dramauh Wrote: The dataframe looks like this
Post a working Dataframe and not a example you have put together.
(Apr-18-2022, 05:53 AM)dramauh Wrote: PD: I am aware that the code looks dirty.
Yes if gone make this this mess the why use Pandas DataFrame in the first place?

Bye working DataFrame i mean like this,so this can you also just copy and run.
import pandas as pd

data = {
    "Name": ["Jai", "Princi", "Gaurav", "Anuj"],
    "Height": [5.1, 6.2, 5.1, 5.2],
    "Qualification": ["Msc", "MA", "Msc", "Msc"],
}

df = pd.DataFrame(data)
address = ["Delhi", "Bangalore", "Chennai", "Patna"]
df["Address"] = address 
Show DataFrame and some export examples.
>>> df
     Name  Height Qualification    Address
0     Jai     5.1           Msc      Delhi
1  Princi     6.2            MA  Bangalore
2  Gaurav     5.1           Msc    Chennai
3    Anuj     5.2           Msc      Patna

>>> df.to_dict()
{'Address': {0: 'Delhi', 1: 'Bangalore', 2: 'Chennai', 3: 'Patna'},
 'Height': {0: 5.1, 1: 6.2, 2: 5.1, 3: 5.2},
 'Name': {0: 'Jai', 1: 'Princi', 2: 'Gaurav', 3: 'Anuj'},
 'Qualification': {0: 'Msc', 1: 'MA', 2: 'Msc', 3: 'Msc'}}

>>> df.to_csv(index=False)
('Name,Height,Qualification,Address\r\n'
 'Jai,5.1,Msc,Delhi\r\n'
 'Princi,6.2,MA,Bangalore\r\n'
 'Gaurav,5.1,Msc,Chennai\r\n'
 'Anuj,5.2,Msc,Patna\r\n')

>>> df.to_string()
('     Name  Height Qualification    Address\n'
 '0     Jai     5.1           Msc      Delhi\n'
 '1  Princi     6.2            MA  Bangalore\n'
 '2  Gaurav     5.1           Msc    Chennai\n'
 '3    Anuj     5.2           Msc      Patna')

>>> print(df.to_xml())
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <Name>Jai</Name>
    <Height>5.1</Height>
    <Qualification>Msc</Qualification>
    <Address>Delhi</Address>
  </row>
  <row>
    <index>1</index>
    <Name>Princi</Name>
    <Height>6.2</Height>
    <Qualification>MA</Qualification>
    <Address>Bangalore</Address>
  </row>
  <row>
    <index>2</index>
    <Name>Gaurav</Name>
    <Height>5.1</Height>
    <Qualification>Msc</Qualification>
    <Address>Chennai</Address>
  </row>
  <row>
    <index>3</index>
    <Name>Anuj</Name>
    <Height>5.2</Height>
    <Qualification>Msc</Qualification>
    <Address>Patna</Address>
  </row>
</data>
Gribouillis likes this post
Reply


Messages In This Thread
export dataframe to file.txt - by dramauh - Apr-18-2022, 05:53 AM
RE: export dataframe to file.txt - by snippsat - Apr-18-2022, 11:01 AM
RE: export dataframe to file.txt - by dramauh - Apr-18-2022, 11:56 AM
RE: export dataframe to file.txt - by snippsat - Apr-18-2022, 06:29 PM
RE: export dataframe to file.txt - by dramauh - Apr-19-2022, 02:23 AM
RE: export dataframe to file.txt - by sarahroxon7 - Apr-21-2022, 01:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 63,436 Mar-12-2021, 01:02 PM
Last Post: buran
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,326 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  Export Co-ords to CSV file gtbiyb 1 1,933 Sep-19-2019, 07:59 PM
Last Post: j.crater
  How to add a dataframe to an existing excel file wendysling 2 28,371 May-09-2019, 07:00 PM
Last Post: wendysling
  convert images into pixel dataframe into csv file using python synthex 3 17,625 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,676 Oct-18-2018, 09:33 PM
Last Post: volcano63
  access a very large file? As an array or as a dataframe? Angelika 5 5,058 May-18-2017, 08:15 AM
Last Post: Angelika

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020