How to read a .tsf file to a dataframe in python pandas? - python

I have a .tsf file.
I want to read it to a dataframe in pandas through a specified path.
How can i do that??

If you by TSF refer to Tab Separated Fields, then you need to use the pandas.read_csv('filename.tsf', sep='\t')
The sep='\t' will tell pandas that the fields are separated by tabs.

Related

How do i create a semicolon separated excel or csv file from the values of a column in PANDAS?

i have an excel file, it has one column, "emails"... i need to get the email values from the column and add them to a row separated by a semicolon.
export or save to either an excel or csv. is this possible in pandas?
Not really sure what your email 'values' are particularly. Once getting them into a pandas df, you can output to .csv with a semi colon delimiter using this code
df.to_csv(sep=';', index=False)

Can Pandas output inferred schema for a CSV file?

Is there a method I can use to output the inferred schema on a large CSV using pandas?
In addition, any way to have it tell me with that type if it is nullable/blank based off the CSV?
File is about 500k rows with 250 columns.
With my new job, I'm constantly being handed CSV files with zero format documentation.
Is it necessary to load the whole csv file? At least you could use the read_csv function if you know the separator or doing a cat of the file to know the separator. Then use the .info():
df = pd.read_csv(path_to_file,...)
df.info()

tabs not getting displayed when writing dataframe to csv in pandas

I have a dataframe where one of the columns contains strings. But they have some tab formatting for each of them. Below is a snippet of how it looks like
formatted_line_items[1:3]
Out[393]: ['\t<string1>', '\t\t<string2>']
However when I write the dataframe using to_csv the formatting is lost. How can I write this to a csv file or excel file and still retain the formatting?
EDIT: I got to know that csv doesn't retain formatting so I used the pandas to_excel function but still no luck with the formatting.
Just found XlsxWriter has a set_indent function where we can specify the indentation.

Unable to get correct output from tsv file using pandas

I have a tsv file which I am trying to read by the help of pandas. The first two rows of the files are of no use and needs to be ignored. Although, when I get the output, I get it in the form of two columns. The name of the first column is Index and the name of second column is a random row from the csv file.
import pandas as pd
data = pd.read_csv('zahlen.csv', sep='\t', skiprows=2)
Please refer to the screenshot below.
The second column name is in bold black, which is one of the row from the file. Moreover, using '\t' as delimiter does not separate the values in different column. I am using Spyder IDE for this. Am I doing something wrong here?
Try this:
data = pd.read_table('zahlen.csv', header=None, skiprows=2)
read_table() is more suited for tsv files and read_csv() is a more specialized version of it. Then header=None will make first row data, instead of header.

How to change tab delimited in to comma delimited in pandas

I don't know if this is something possible. I am trying to append 12 files into a single file. One of the files is tab delimited and the rest comma delimitted. I loaded all the 12 files into dataframe and append it into an empty dataframe one by one in a loop.
list_of_files = glob.glob('./*.txt')
df = pd.DataFrame()
for filename in list_of_files:
file = pd.read_csv(filename)
dfFilename = pd.DataFrame(file)
df = df.append(dfFilename, ignore_index=True)
But the big file is not in the format I wanted it to be. And I think the problem is with the tab delimited file. And I tried to run the code without the tab delimited file and the format of the appended file is fine. So I was thinking if it is possible to change the tab delimited format into comma delimited using pandas.
Thank you for your help and suggestion
You need to tell Pandas that the file is tab delimited when you import it. You can pass a delimiter to the read_csv method but in your case, since the delimiter changes by file, you want to pass None - this will make Pandas auto-detect the correct delimiter.
Change your read_csv line to:
pd.read_csv(filename,sep=None)
For the file that is tab-separated, you should use:
file = pd.read_csv(filename, sep="\t")
Pandas read_csv has quite a lot of parameters, check it out in the docs

Categories

Resources