I want to format data from a text file to a specific format. My data file contains more than 120000 rows but I have posted here truncated data. The data file has R, L, G, C data for different frequencies (here 3 frequency in 3 rows). The file has only 2 columns 1st column is "Freq" and 2nd column is either one of the RLGC data. Now I want to manipulate the data to another format (Let's say, the target .txt). here is the link of data. I want to convert it to target file like this.
Here is my code:
import pandas as pd
#create DataFrame from csv with columns f and v
df = pd.read_csv('data_in.txt', sep="\s+", names=['freq','v'])
#df = df.astype(float).convert_objects()
#boolean mask for identify columns of new df
m = df['v'].str.endswith('R', 'L', 'G', 'C')
#new column by replace NaNs by forward filling
df['g'] = df['v'].where(m).ffill()
#get original ordering for new columns
cols = df['g'].unique()
#remove rows with same values in v and g columns
df = df[df['v'] != df['g']]
#reshape by pivoting with change ordering of columns by reindex
df = df.pivot('freq', 'g', 'v').rename_axis(None, axis=1).reindex(columns=cols).reset_index()
df.columns = [x.replace('R','R1:1').replace('L','L1:1').replace('G','G1:1').replace('C','C1:1') for x in df.columns]
df.to_csv('target.txt', index=False, sep='\t')
But it gives the following error:
TypeError: wrapper3() takes from 2 to 3 positional arguments but 5 were given
Can anyone help me to format it into target file.
Now I need another formatting other than target file. I need to format into like "target_2.txt". This is another unusual type of format that is also needed. You can see that each of the R1:1, L1:1, G1:1 and C1:1 data now seem like a block of array (though not an array). If you look closely, for freq, it should names as FORMAT Freq, then a tab, then :, then again a tab and then R1:1. If you see, it will be like - FORMAT Freq+tab+:+tab+R1:1. Then a new line, then 2 tabs, then L1:1. Then again a new line, then 2 tabs, then G1:1. And, finally the same for C1:1. After that a blank line, then follows the 1st row of data, 2nd row of data and continues. The data values will be according to the header line.
How to do that 2nd target file?
I am using Spyder 3.2.6 where python 3.6.4 64-bit is embedded.