Search code examples
pythonpandasdataframeflattencolumnsorting

Modify and flatten values from Pandas dataframe


Here is the dataframe I am working with:

            0
0  380.143752
1  379.942595
2  379.589472
3  379.816187
4  379.622086
5  379.299071
6  379.559615

dtypes gives this:

0    float64
dtype: object

You can get a sample of the data by click on the link below:

https://ufile.io/x534q

What I would like to do now is to get rid of the header, the first column (0 to 6) and to flatten the rest of values so that the end result looks like this:

380.143752 379.942595 379.589472 379.816187 379.622086 379.299071 379.559615

Could you please help me? Thanks in advance.


Solution

  • A Transpose of the dataframe should give you what you need.

    newdf = (pd.DataFrame(df)).T
    

    This will still have headers though but you can use the data in your normal operations ignoring the header and index

    e.g. write to a csv file

    newdf.to_csv(file,mode='w', sep=',',header=False,index=False) # you can use any separator here 'tabs' if you don't want commas.