简体   繁体   中英

Pandas Dataframe header do not match correct column

I print pandas dataframe with this code

df=pd.read_csv(pd.compat.StringIO(output),skiprows=[1,2,3,4,5,6,7,9])

new_header = df.iloc[0]
df = df[1:]
df.columns = new_header

print(df)

It show output like this. The dataframe header do not match correct column. ID header not on the column 2.

0             ID    MAC            Name              Group   IP             Type            State  STA Uptime          ExtraInfo
1    0     10c0-ffff-0000 TEST-A1 @1ABCD -              AP12345        idle   0   -               -                 
2    1     10c0-ffff-000f TEST-A2 @2ABCD 10.20.0.1      AP12345        nor    1   12D:5H:10M:16S  - 

I use print(df.columns) it show output like this. It look like header not separate.

Index(['ID    MAC            Name              Group   IP             Type            State  STA Uptime          ExtraInfo'], dtype='object', name=0)

I export to csv it show data in same line like this picture. 在此处输入图片说明

So I edit code by using delim_whitespace like this.

df=pd.read_csv(pd.compat.StringIO(output),skiprows=[1,2,3,4,5,6,7,9],delim_whitespace=True)

The output of print(df.columns) show header like this.

Index(['STA', 'Uptime', 'ExtraInfo'], dtype='object', name=('ID', 'MAC', 'Name', 'Group', 'IP', 'Type', 'State'))

I export data to csv but it show header 3 column only like this picture.

在此处输入图片说明

I print(df) it show output like this.

('ID', 'MAC', 'Name', 'Group', 'IP', 'Type', 'State')
STA  ... ExtraInfo
0
10c0-ffff-0000 TEST-A1        @1ABCD -              AP12345 idle  0    ...  -       
1
10c0-ffff-000f TEST-A2        @1ABCD 10.20.0.1      AP12345 nor   0    ...  -   

Why dataframe header do not match correct column? How to fix it?

From the data you described, you can just combine two approaches, you just have to turn the header off, and then do, as you did in the first attempt. I hope this will work out for you!

df=pd.read_csv(pd.compat.StringIO(output), header=None, skiprows=[1,2,3,4,5,6,7,9], delim_whitespace=True)

new_header = df.iloc[0]
df = df[1:]
df.columns = new_header

print(df.columns)

除非我理解不正确,否则可以用1行完成:

df=pd.read_csv(pd.compat.StringIO(output),skiprows=[1,2,3,4,5,6,7,9], header=None,names=['ID', 'MAC', 'Name', 'Group', 'IP', 'Type', 'State'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM