【问题标题】:How do you remove sections from a csv file using pandas?如何使用 pandas 从 csv 文件中删除部分?
【发布时间】:2020-10-16 16:17:34
【问题描述】:

我正在按照这个项目指南进行操作,但我到达了一个部分,我不确定代码是如何工作的。有人可以解释一下下面的代码块吗:

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.

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

【问题讨论】:

  • axis =1 表示按列放置,axis=0 表示行。就地意味着新的数据框被分配给相同的变量名。当您打印 df 时,您可以看到更改。
  • inplace 意味着对 df 的操作让我们说将直接对持有它的变量进行;它不需要重新分配,轴决定你是删除行还是整列;

标签: python pandas csv file


【解决方案1】:

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

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

在列表中我们提到要删除的列的位置,axis = 1 表示按列删除它们,并且在适当的位置只会使这种更改实际发生在原始数据帧上成为永久更改。

上面的命令也可以写成

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

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

【讨论】:

    猜你喜欢
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2016-09-12
    • 2017-01-10
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多