0

I have a txt file with info inside of it, separated for every deal with \n symbol.

DEAL: 896
CITY: New York
MARKET: Manhattan
PRICE: $9,750,000
ASSET TYPE: Rental Building
SF: 8,004
PPSF: $1,218
DATE: 11/01/2017

Is there any way to make a csv (or another) table with headers, specified like CITY, MARKET, etc. with pandas or csv module? All the info from specific title should go into corresponding header

1
  • 1
    check pandas.pivot Commented Dec 6, 2018 at 18:09

2 Answers 2

1

Updated to navigate around using : as a delimiter:

import pandas as pd
new_temp = open('temp.txt', 'w') # writing data to a new file changing the first delimiter only
with open('fun.txt') as f:
    for line in f:
        line = line.replace(':', '|', 1) # only replace first instance of : to use this as delimiter for pandas
        new_temp.write(line)
new_temp.close()

df = pd.read_csv('temp.txt', delimiter='|', header=None)
df = df.set_index([0]).T
df.to_csv('./new_transposed_df.csv', index=False)

Will make a csv with the left column as headers and the right column as data without changing colons in the second column. It will write out a temp file called temp.txt which you can remove after you run the program.

Sign up to request clarification or add additional context in comments.

9 Comments

pandas.errors.ParserError: Error tokenizing data. C error: Expected 2 fields in line 10, saw 3
this works only if your data is as you posted it. I copied your example into a text file and ran this so if there is some deviation from what you posted it may not work. I also don't know what line 10 is in your code as what I posted is 3 lines.
Oh, I get it now. Yeah, there are like 700 of such deals, some with different headers
It appears that one (or some) of the deals in the file have a : in column 1 or column 2 (for example if you had one that said Time: 9:00 am, 3 which would create 3 columns instead of 2 and just fill the 2nd column with 9 and the 3rd column with 00 am for those :P. Cleaning up your data is half the battle!
You right, thanks for everything :) Appreciate all the help!
|
1

Use Pandas to input it and then transform/pivot your table.

import pandas as pd

df = pd.read_csv('data.txt',sep=':',header=None)

df = df.set_index(0).T

Example

import pandas as pd

data = '''
DEAL: 896
CITY: New York
MARKET: Manhattan
PRICE: $9,750,000
ASSET TYPE: Rental Building
SF: 8,004
PPSF: $1,218
DATE: 11/01/2017
'''

df = pd.read_csv(pd.compat.StringIO(data),sep=':',header=None)

print(df.set_index(0).T)

Results:enter image description here

2 Comments

you edited your code to be the exact same as mine, haha.
;) I did not know there was another answer. I am on mobile ;) After writing the answer I went and test it on online Jupyter Notebook, tryjupyter, and ended updating my code. Great minds thinks the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.