0

How i can make from this DataFrame: (try df.drop([0]), df.iloc[1:] but dont work)

     A B C
  0  1 2 3
  1  X Y Z

this:

     1 2 3
  0  X Y Z
1

2 Answers 2

5

in two steps,

df.columns = df.iloc[0]

df = df.iloc[1:].reset_index(drop=True)


print(df)

   1  2  3
0  X  Y  Z

a better method would be to use skiprows in your read argument.

from io import StringIO
d = """   A B C
    1 2 3
    X Y Z"""

df = pd.read_csv(StringIO(d),sep='\s+',skiprows=1)
print(df)

   1  2  3
0  X  Y  Z
Sign up to request clarification or add additional context in comments.

Comments

3

IIUC, DataFrame.transpose and DataFrame.set_index

df.T.set_index(0).T.reset_index(drop=True).rename_axis(columns=None)
   1  2  3
0  X  Y  Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.