简体   繁体   中英

How to remove ugly row in pandas.dataframe

so I am filling dataframes from 2 different files. While those 2 files should have the same structure (the values should be different thought) the resulting dataframes look different. So when printing those I get:

                a              b              c        d
0        70402.14   70370.602112       0.533332       98
1        31362.21   31085.682726       1.912552      301       
...           ...            ...            ...      ...
753919   64527.16   64510.008206       0.255541       71
753920   58077.61   58030.943621       0.835758      152

                a              b              c        d
index
0       118535.32  118480.657338       0.280282       47
1        49536.10   49372.999416       0.429902       86
...           ...            ...            ...      ...
753970   52112.95   52104.717927       0.356051      116
753971   37044.40   36915.264944       0.597472      165

So in the second dataframe there is that "index" row that doesnt make any sense for me and it causes troubles in my following code. I did neither write the code to fill the files into the dataframes nor I did create those files. So I am rather interested in checking if such a row exists and how I might be able to remove it. Does anyone have an idea about this?

The second dataframe has an index level named "index". You can remove the name with

df.index.name = None

For example,

In [126]: df = pd.DataFrame(np.arange(15).reshape(5,3))    

In [128]: df.index.name = 'index'

In [129]: df
Out[129]: 
        0   1   2
index            
0       0   1   2
1       3   4   5
2       6   7   8
3       9  10  11
4      12  13  14

In [130]: df.index.name = None

In [131]: df
Out[131]: 
    0   1   2
0   0   1   2
1   3   4   5
2   6   7   8
3   9  10  11
4  12  13  14

The dataframe might have picked up the name "index" if you used reset_index and set_index like this:

In [138]: df.reset_index()
Out[138]: 
   index   0   1   2
0      0   0   1   2
1      1   3   4   5
2      2   6   7   8
3      3   9  10  11
4      4  12  13  14

In [140]: df.reset_index().set_index('index')
Out[140]: 
        0   1   2
index            
0       0   1   2
1       3   4   5
2       6   7   8
3       9  10  11
4      12  13  14

索引只是第一列-默认情况下它为行编号,但是您可以通过多种方式更改它(例如,用其中一列的值填充它)

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