I read a CSV file with pandas.read_csv("sample.csv", sep=";") and got this output:
However I want to get my dataframe output like this:
Is this possible?
You can try 'tabulate' library. Maybe not exactly the same layout you show, but might help.
Related
I am starting to learn and understand panda module in Python. However, my issue is with the rename string. The rename works fine when i use print, this shows the column has been renamed:
print(data.rename(columns={"Rep": "Name"}))
However, when i use print(data), to show all of the data from the document, the column does not show as being renamed. This also does not show when the file has been exported using the data.to_csv("example.csv") string.
Would really appreciate if somebody could shed some light on this please.
Full Source code below:
import pandas as pd
data = pd.read_excel(r"D:\Downloads\Book1.xlsx")
del data["Region"]
del data["Item"]
print(data.rename(columns={"Rep": "Name"})
print(data)
data.to_csv("example.csv")
Use inplace argument, to make the changes reflect in the DataFrame as well, like this:
data.rename(columns={"Rep": "Name"}, inplace = True)
Try adding 'inplace=True' to data.rename
print(data.rename(columns={"Rep": "Name"}, inplace=True))
I am exporting a pandas dataframe as an excel file from a tutorial, however the resulting file does not include the highlighting and I have no idea why.
To style it:
df_styled = df.style.apply(lambda x: ['background: orange' for x in df.Margin_rate], axis=0)
and then to export it:
df_styled.to_excel('excel_python_tutorial_marked.xlsx', engine='openpyxl', index=False)
I have made sure to create a new df to export it and everything, where am I going wrong?
Because it's meant to look like this:
But instead it looks normal in excel:
Apparently you need to pass style information explicitly into the openpyxl writer. Maybe this helps.
I have had a good experience with the following, but you might need additional packages and restructure your code a little: https://xlsxwriter.readthedocs.io/example_pandas_column_formats.html
I have a CSV file, diseases_matrix_KNN.csv which has excel table.
Now, I would like to store all the numbers from the row like:
Hypothermia = [0,-1,0,0,0,0,0,0,0,0,0,0,0,0]
For some reason, I am unable to find a solution to this. Even though I have looked. Please let me know if I can read this type of data in the chosen form, using Python please.
most common way to work with excel is use Pandas.
Here is example:
import pandas as pd
df = pd.read_excel(filename)
print (df.iloc['Hypothermia']). # gives you such result
I have a messy text file that I need to sort into columns in a dataframe so I
can do the data analysis I need to do. Here is the messy looking file:
Messy text
I can read it in as a csv file, that looks a bit nicer using:
import pandas as pd
data = pd.read_csv('phx_30kV_indepth_0_0_outfile.txt')
print(data)
And this prints out the data aligned, but the issue is that the output is [640 rows x 1 column]. And I need to separate it into multiple columns and manipulate it as a dataframe.
I have tried a number of solutions using StringIO that have worked here before, but nothing seems to be doing the trick.
However, when I do this, there is the issue that the
delim_whitespace=True
Link to docs ^
df = pd.read_csv('phx_30kV_indepth_0_0_outfile.txt', delim_whitespace=True)
Your input file is actually not in CSV format.
As you provided only .png picture, it is even not clear, whether this file
is divided into rows or not.
If not, you have to start from "cutting" the content into individual lines and
read the content from the output file - result of this cutting.
I think, this is the first step, before you can use either read_csv or read_table (of course, with delim_whitespace=True).
I am trying to restructure the way my precipitations' data is being organized in an excel file. To do this, I've written the following code:
import pandas as pd
df = pd.read_excel('El Jem_Souassi.xlsx', sheetname=None, header=None)
data=df["El Jem"]
T=[]
for column in range(1,56):
liste=data[column].tolist()
for row in range(1,len(liste)):
liste[row]=str(liste[row])
if liste[row]!='nan':
T.append(liste[row])
result=pd.DataFrame(T)
result
This code works fine and through Jupyter I can see that the result is good
screenshot
However, I am facing a problem when attempting to save this dataframe to a csv file.
result.to_csv("output.csv")
The resulting file contains the vertical index column and it seems I am unable to call for a specific cell.
(Hopefully, someone can help me with this problem)
Many thanks !!
It's all in the docs.
You are interested in skipping the index column, so do:
result.to_csv("output.csv", index=False)
If you also want to skip the header add:
result.to_csv("output.csv", index=False, header=False)
I don't know how your input data looks like (it is a good idea to make it available in your question). But note that currently you can obtain the same results just by doing:
import pandas as pd
df = pd.DataFrame([0]*16)
df.to_csv('results.csv', index=False, header=False)