Import CSV file into Python [duplicate] - python

This question already has answers here:
How do I read and write CSV files with Python?
(7 answers)
Closed 4 years ago.
I tried several times to import CSV file into python 2.7.15 but it fail.
Please suggest how to import CSV in Python.
Thank you

There are two ways to import a csv file in Python.
First: Using standard Python csv
import csv
with open('filename.csv') as csv_file:
csv_read=csv.reader(csv_file, delimiter=',')
Second: Using pandas
import pandas as pd
data = pd.read_csv('filename.csv')
data.head() # to display the first 5 lines of loaded data
I would suggest to import using pandas since that's the more convenient way to do so.

Related

Is it possible to save a string variable in the csv format as a python dataframe WITHOUT first saving a csv? [duplicate]

This question already has answers here:
How to use StringIO in Python3?
(9 answers)
How do I write data into CSV format as string (not file)?
(6 answers)
Closed 9 months ago.
I have a string that is in the csv format, and I am wondering whether I can save it as a data frame without first saving it as a csv and then importing it as one.
Thanks!
Yes, one of the possible approaches is to convert the string into a StringIO object and pass it to pd.DataFrame
from io import StringIO
import pandas as pd
string_data = StringIO("""col1,col2
1,"a"
2,"b"
3,"c"
4,"d"
""")
df = pd.read_csv(string_data)

Editing .mat file in Python [duplicate]

This question already has answers here:
Read .mat files in Python
(15 answers)
reading v 7.3 mat file in python
(9 answers)
Closed last year.
I am trying to import a .mat (not human readable) file in python
I imported it using a package called AT.
import at
from at.plot import plot_beta
import time
import matplotlib.pyplot as plt
r = at.load_mat('FCCee_z_512_nosol_10.mat', key='RING')
Now, I want to access the data inside this .mat file and also to add some values to it using python and to save the updated file in .py format.
The result of printing the r is :
Lattice(<11470 elements>, name='l000015', energy=45600000000.0, particle=Particle('electron'), `periodicity=1, harmonic_number=121657, mat_key='RING', mat_file='N:\\musa\\1\\1\\General-`quistions\\MAD-X_Python\\fccV8_AT\\FCCee_z_512_nosol_10.mat', key='ring')`
Ho can i edit the valus of this file and save in in python format ?
You can use scipy loadmat method to read .mat files.
https://docs.scipy.org/doc/scipy-1.8.0/html-scipyorg/reference/generated/scipy.io.loadmat.html?highlight=loadmat#scipy.io.loadmat
import scipy.io
r = scipy.io.loadmat('FCCee_z_512_nosol_10.mat')
r['data'] # you want to access

Can I save a table/dataframe to a file (like png/jpg) in python? [duplicate]

This question already has answers here:
How to save a pandas DataFrame table as a png
(13 answers)
Closed 1 year ago.
I found several ways to PRINT tables in nicer formatting but can I also SAVE those outputs to a file (not csv, excel etc.)? They don't even need to be changeable, an image-like representation would be great. I get presentation-ready dataframes that I have to reformat since I'm saving them in excel files at the moment.
Assuming this table is a pandas DataFrame, this library might help:
www.dexplo.org/dataframe_image/
This library would export pandas DataFrames in a jupyter notebook fashioned way.
Example usage:
import pandas as pd
import dataframe_image as dfi
df = pd.DataFrame({'key':[1,2,3],'val':['a','b','c']})
dfi.export(df, 'dataframe.png')

How to convert 100's of .log files to .xlsx files [duplicate]

This question already has answers here:
How can I iterate over files in a given directory?
(11 answers)
Closed 2 years ago.
This a a portion of a larger code.
I have a directory with 100's of .log files that I need to convert to .xlsx files one at a time. I wrote this code;
import csv
import glob
import pandas as pd
df = pd.read_csv('input.log', delimiter=r"\s+", header=None, names=list(range(20)))
df.to_excel('input.xlsx', 'Sheet1')
Which works for a single file. What do I need to add to have it look through the directory and convert each file regardless of how many there are?
import glob
import pandas as pd
files = glob.glob("*.log")
for file in files:
df = pd.read_csv(file, delimiter=r"\s+", header=None, names=list(range(20)))
df.to_excel(file+'.xlsx', index=Flase)

Downloading a Web File directly into Pandas [duplicate]

This question already has answers here:
Pandas read_csv from url
(6 answers)
Closed 3 years ago.
I am trying to download a web file with this link in pandas. The issue that I am having is that most tutorials show a file that can be downloaded with a particular extension on the end, which allows for you to more easily directly download it.
This link results in the download of a text file, but it cannot be easily read with conventional methods. How can I download this file directly in pandas with this link.
Data Science Acolyte, all you have to do is this!
import pandas as pd
df = pd.read_csv('ADAMS.txt')
you can try:
df = pd.read_csv('https://www6.sos.state.oh.us/ords/f?p=VOTERFTP:DOWNLOAD::FILE:NO:2:P2_PRODUCT_NUMBER:1')
Please try this code below if it works for you:
import pandas as pd
import io
import requests
url="https://www6.sos.state.oh.us/ords/f?p=VOTERFTP:DOWNLOAD::FILE:NO:2:P2_PRODUCT_NUMBER:1"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
More details here.

Categories