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 
7
  • 2
    df.columns = ['Power', 'Speed'] ? Commented Nov 17, 2021 at 19:41
  • It look slike you have a multiindex perhaps? Try df.reset_index() Commented Nov 17, 2021 at 19:43
  • @not_speshal no because I don't want to rename. I want to add because I only have one column Commented Nov 17, 2021 at 19:47
  • I already tried what @splash58 did but I received the following error: ValueError: Length mismatch: Expected axis has 1 elements, new values have 2 elements Commented Nov 17, 2021 at 19:50
  • @HenryEcker Done that Commented Nov 17, 2021 at 19:53

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.