【问题标题】:How to delete a Column by comparing the Length of the Row?如何通过比较行的长度来删除列?
【发布时间】:2021-04-15 01:42:04
【问题描述】:

Hello World,如果特定 Item 的 Length 小于 5 或​​ Item 为 Empty,如何删除整个列? 这就是我使用列表中的熊猫保存到 CSV 的方式:

import pandas as pd
df = pd.DataFrame(Entertainmentlist)
df.to_csv('EnterTainment.csv', encoding='utf-8-sig' , index = False , mode = 'a')

这是 CSV 文件的屏幕截图:

正如您在 Column 3heading is emptyhas length <5 中看到的那样

如果标题为空或长度 ,我如何循环获取整个 CSV 并删除完整列 including image and description

【问题讨论】:

标签: python csv beautifulsoup


【解决方案1】:

您可以执行以下操作,无需删除任何,只需过滤您的df,然后将其写入csv

示例

import pandas as pd

cars = {'Brand': ['Honda Civic','Kia','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000]
        }

df = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df_filtered = df[df['Brand'].map(len) > 5]

df_filtered.to_csv('cars.csv', encoding='utf-8-sig' , index = False , mode = 'a')

如果你真的想删除 ,请使用.drop()

df.drop(df[df['Brand'].map(len) < 5].index, inplace=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2014-12-25
    相关资源
    最近更新 更多