Pandas program to import and export data between Pandas and CSV file.

Write a program to import and export data between Pandas and CSV file with Practical Example.

1.To Export data between Pandas and CSV file

2.To Import data between Pandas and CSV file

  1. Write a program to Export data between dataframe and CSV with practical example

Program Logic:

  • Import pandas module in program using import statement
  • Import numpy module using import statement
  • Create Dictionary object say ‘marks’ with different set of key-value pair
  • Create DataFrame object say ‘result’ using DataFrame method and pass index as argument to it
  • Print DataFrame ‘result’ using print function
  • Write dataframe object into csv file using to_csv method and pass data frame object as an argument to it

Below is implementation code/Source code:

import pandas as pd
import numpy as np
marks  = { "English" :[67,89,90,55],
           "Maths":[55,67,45,56],
            "IP":[66,78,89,90],
           "Chemistry" :[45,56,67,65],
           "Biology":[54,65,76,87]}
result = pd.DataFrame(marks,index=["Athang","Sujata","Sushil","Sumedh"])
print("******************Marksheet****************")
print(result)
result.to_csv("result.csv")

Below is Output:

*****************Marksheet****************
        English  Maths  IP  Chemistry  Biology
Athang       67     55  66         45       54
Sujata       89     67  78         56       65
Sushil       90     45  89         67       76
Sumedh       55     56  90         65       87

Below is Snapshot of executable code with output:

Below is result.csv file

2. Write a program to import data between pandas and CSV file with Practical Example

Program Logic:

  1. Import pandas module in program using import statement
  2. Read data from CSV file using read_csv method and pass CSV file say ‘result.csv” as an argument to it
  3. Store data which is read from CSV file in python object say ‘df’
  4. Display data as output using print function

Below is implementation code/Source code

import pandas as pd
# read data from CSV file
df = pd.read_csv("result.csv")
print(df)

Below is Output:

Unnamed: 0  English  Maths  IP  Chemistry  Biology
0     Athang       67     55  66         45       54
1     Sujata       89     67  78         56       65
2     Sushil       90     45  89         67       76
3     Sumedh       55     56  90         65       87


Below is Snapshot of executable code with output:

Below is result.csv file

You can also check this too:

<