如何使用熊猫从csv文件中删除节?

时间:2020-06-26 05:08:31

标签: python pandas csv file

我正在跟着这个项目指南一起去,我到达了一段我不确定代码如何工作的地方。有人可以解释以下代码块吗?

to_drop = ['Edition Statement',
       'Corporate Author',
       'Corporate Contributors',
       'Former owner',
       'Engraver',
       'Contributors',
       'Issuance type',
       'Shelfmarks']

df.drop(to_drop, inplace=True, axis=1)

这是在执行前面的代码之前csv文件的格式:

    Identifier             Edition Statement      Place of Publication  \
0         206                           NaN                    London
1         216                           NaN  London; Virtue & Yorston
2         218                           NaN                    London
3         472                           NaN                    London
4         480  A new edition, revised, etc.                    London

  Date of Publication              Publisher  \
0         1879 [1878]       S. Tinsley & Co.
1                1868           Virtue & Co.
2                1869  Bradbury, Evans & Co.
3                1851          James Darling
4                1857   Wertheim & Macintosh

                                           Title     Author  \
0                  Walter Forbes. [A novel.] By A. A      A. A.
1  All for Greed. [A novel. The dedication signed...  A., A. A.
2  Love the Avenger. By the author of “All for Gr...  A., A. A.
3  Welsh Sketches, chiefly ecclesiastical, to the...  A., E. S.
4  [The World in which I live, and my place in it...  A., E. S.

                               Contributors  Corporate Author  \
0                               FORBES, Walter.               NaN
1  BLAZE DE BURY, Marie Pauline Rose - Baroness               NaN
2  BLAZE DE BURY, Marie Pauline Rose - Baroness               NaN
3                   Appleyard, Ernest Silvanus.               NaN
4                           BROOME, John Henry.               NaN

   Corporate Contributors Former owner  Engraver Issuance type  \
0                     NaN          NaN       NaN   monographic
1                     NaN          NaN       NaN   monographic
2                     NaN          NaN       NaN   monographic
3                     NaN          NaN       NaN   monographic
4                     NaN          NaN       NaN   monographic

                                      Flickr URL  \
0  http://www.flickr.com/photos/britishlibrary/ta...
1  http://www.flickr.com/photos/britishlibrary/ta...
2  http://www.flickr.com/photos/britishlibrary/ta...
3  http://www.flickr.com/photos/britishlibrary/ta...
4  http://www.flickr.com/photos/britishlibrary/ta...

                        Shelfmarks
0    British Library HMNTS 12641.b.30.
1    British Library HMNTS 12626.cc.2.
2    British Library HMNTS 12625.dd.1.
3    British Library HMNTS 10369.bbb.15.
4    British Library HMNTS 9007.d.28.

代码的哪一部分告诉熊猫删除列而不是行? inplace = True和axis = 1是什么意思?

1 个答案:

答案 0 :(得分:0)

这确实是Pandas数据框架中的基础,我想您应该学习免费的教程。无论如何,此代码块都会删除存储在to_drop中的列。 到目前为止,我们使用此命令删除了名称为df的数据框

df.drop([], inplace=True), axis=1,

在列表中我们提到了要删除的列,轴= 1意味着将它们逐列放置,并且就位使其成为永久更改,这种更改实际上将在原始数据帧上发生。

您也可以将上述命令编写为

df.drop(['Edition Statement',
   'Corporate Author',
   'Corporate Contributors',
   'Former owner',
   'Engraver',
   'Contributors',
   'Issuance type',
   'Shelfmarks'], inplace=True, axis=1)

这里是熊猫的基本指南,供您将来查询Introduction to pandas