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
Related
This question already has answers here:
Output images to html using python
(3 answers)
Dynamically serving a matplotlib image to the web using python
(6 answers)
Closed 4 months ago.
I wrote a function to visualize python graphs using 'Seaborn' & 'Matplotlib' & I have been trying to store the output in HTML files. Is there a way to do so? I looked online but couldn't find any sources that talk about storing the visualizations of these libraries in HTML.
# Importing libraries
from statistics import correlation
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
def dynamic_manual_eda(df):
# Visualizing missing values
miss_viz=sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')
miss_plot=miss_viz.write_html("templates/missing_viz.html")
return miss_plot
eda_file = pd.read_csv('C:/Users/Churn_bank.csv')
dynamic_manual_eda(eda_file)
The error I get here is - '**AttributeError: 'AxesSubplot' object has no attribute 'write_html**''
NOTE: The visualizations should be stored only in HTML files. That's the requirement.
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)
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')
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.
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.