Home > Software design >  How to read in or transform txt file in order to get specific dataframe (every three paragraphs in o
How to read in or transform txt file in order to get specific dataframe (every three paragraphs in o

Time:02-24

I have such a text file:

Count1
------
1234

Count2
------
2345

Count3
------
3456

What I want to get is a dataframe like this one

Count1 1234
Count2 2345
Count3 3456

That means I want to get a dataframe which rows exist of the three paragraphs (excluding the second paragraph) and makes a new row after each third paragraph in the original text file.

Is this possible by reading in the dataframe in a speacial way or do I have to transform the dataframe after just reading in my txt-file as csv?

CodePudding user response:

Based on the data, it looks like you would need to transform the data after reading from the text file.

Below is the code you can try where test.txt file contains the data you have mentioned in your question.

with open('test.txt', 'r') as f:
    data = f.read().splitlines()
data = [x for x in data if ''.join(set(x)) not in ['','-']]
df = pd.DataFrame(data[1:len(data):2], data[0:len(data):2]).reset_index()
df.rename(columns={'index' : 'Count', 0 : 'Value'}, inplace=True)
print(df)

    Count Value
0  Count1  1234
1  Count2  2345
2  Count3  3456
  • Related