0

I have a DataFrame wich has 2 'columns'. The first column does not seem to have a column name, the second one is named Speed.

Here is a MRE:

from io import StringIO  # to read strings as files for read_csv

import pandas as pd

parts = [
    '[Level1]\nLocation = "London"\nType= "GTHY66"\n'
    'Date = "16-11-2021"\nEnergy level = "Critical zero"\n',
    '0.000   26.788\n0.027   26.807\n0.053   26.860'
]

lvl2_lines = "Speed\n" + parts[1]

df_level2 = pd.read_csv(StringIO(lvl2_lines), sep='\t')
print(df_level2.columns)
print(df_level2)

This was my output when I did the print statements:

Index(['Speed'], dtype='object')
            Speed
0  0.000   26.788
1  0.027   26.807
2  0.053   26.860

This is my desired output:

Index(['Power', 'Speed'], dtype='object')
   Power   Speed
0  0.000  26.788
1  0.027  26.807
2  0.053  26.860
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Al-Andalus
  • 115
  • 1
  • 8

1 Answers1

1

You need to split the strings:

df[["Power", "Speed"]] = df["Speed"].str.split(expand=True).astype(float)

>>> df
    Speed  Power
0  26.788  0.000
1  26.807  0.027
2  26.860  0.053
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • We could just specify both column names `lvl2_lines = "Speed\n" + parts[1]` -> `lvl2_lines = "Power Speed\n" + parts[1]` then there's no need to split after the fact or change dtype. But this would push the question into typo territory. And `df_level2 = pd.read_csv(StringIO(lvl2_lines), sep=r'\s+', engine='python')` to handle mixed spacing. – Henry Ecker Nov 17 '21 at 20:00
  • I see what you mean. That's a neat way to directly read correctly instead of manipulating later. I answered before OP posted that bit in his question tho :) – not_speshal Nov 17 '21 at 21:10