pandas DataFrame显示数据的问题

qhhrdooz  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在处理一门课程的作业,我的数据框遇到了一个问题。我按照他们的要求做了修改,但是当我去显示我的新数据框时,它只显示标题。
以下是任务的要求:
1.使用Pandas加载数据文件
1.检查数据中是否存在空值。
1.删除任何列中包含空值的记录

  1. Size列的大小以KB和MB为单位。要进行分析,您需要将它们转换为数值
  • 数据集的M和K以及“因设备而异”显示在这些列中,因此我将其删除

1.价格字段为字符串,并带有$符号。请删除$符号并转换为数值。
1.平均评分应介于1和5之间,因为只允许使用这些值。删除值超出此范围的行。
1.对于类型列中的免费应用程序,删除这些行。

这是我的代码:

import pandas as pd
import numpy as np

ds = pd.read_csv('googleplaystore.csv')

headers = pd.DataFrame(['App', 'Category', 'Rating', 'Reviews', 'Size', 'Installs', 'Type', 'Price', 'Content Rating', 'Genres', 'Last Updated', 'Current Ver', 'Android Ver'])

ds['Size'] = ds['Size'].replace("Varies with Device", np.nan, inplace = True)

ds =ds.dropna()

ds['Size'] = ds['Size'].str.replace("M", "", regex = True)
    
ds['Size'] = ds['Size'].str.replace("k", "", regex = True)

ds['Size'] = ds['Size'].astype(float)

ds['Installs'] = ds['Installs'].str.replace("+", '', regex = True)

ds['Installs'] = ds['Installs'].astype(int)

ds['Reviews'] = ds['Reviews'].astype(float)

ds['Price'] = ds['Price'].str.replace("$", "", regex = True)

ds['Price'] = ds['Price'].astype(float)

indexrating = ds[(ds['Rating'] >= 1) & (ds['Rating'] <= 5)].index

ds.drop(indexrating, inplace = True)

ds['Type']= ds['Type'].replace("Free", np.nan, inplace = True)

ds =ds.dropna()

display(ds)

我希望新的 Dataframe 与删除的行一起显示

pokxtpni

pokxtpni1#

删除以“M”或“k”结尾或包含“随设备而变化”的所有内容,删除所有行。

>>> df['Size'].str[-1].value_counts()
M    7466  # ends with 'M'
e    1637  # ends with 'k'
k     257  # for "Varies with device"
Name: Size, dtype: int64

尝试使用此版本:

df = pd.read_csv(googleplaystore.csv)  # 1
df = df.dropna()  # 3
df['Size'] = df['Size'].str.extract(r'(\d+\.?\d)', expand=False).astype(float) * df['Size'].str[-1].replace({'M': 1024, 'k': 1})  # 4
df = df.dropna()  # remove nan from "Varies with device"
df['Price'] = df['Price'].str.strip('$').astype(float)  # 5
df = df.loc[df['Rating'].between(1, 5)]  # 6
df = df.loc[df['Type'] != 'Free']  # 7

输出:

>>> df
                                                 App            Category  Rating Reviews     Size  Installs  Type  Price Content Rating            Genres       Last Updated Current Ver Android Ver
234    TurboScan: scan documents and receipts in PDF            BUSINESS     4.7   11442   6963.2  100,000+  Paid   4.99       Everyone          Business     March 25, 2018       1.5.2  4.0 and up
235                   Tiny Scanner Pro: PDF Doc Scan            BUSINESS     4.8   10295  39936.0  100,000+  Paid   4.99       Everyone          Business     April 11, 2017       3.4.6  3.0 and up
290    TurboScan: scan documents and receipts in PDF            BUSINESS     4.7   11442   6963.2  100,000+  Paid   4.99       Everyone          Business     March 25, 2018       1.5.2  4.0 and up
291                   Tiny Scanner Pro: PDF Doc Scan            BUSINESS     4.8   10295  39936.0  100,000+  Paid   4.99       Everyone          Business     April 11, 2017       3.4.6  3.0 and up
477                                       Calculator              DATING     2.6      57   6348.8    1,000+  Paid   6.99       Everyone            Dating   October 25, 2017       1.1.6  4.0 and up
...                                              ...                 ...     ...     ...      ...       ...   ...    ...            ...               ...                ...         ...         ...
10690                                       FO Bixby     PERSONALIZATION     5.0       5    861.0      100+  Paid   0.99       Everyone   Personalization     April 25, 2018         0.2  7.0 and up
10697                                        Mu.F.O.                GAME     5.0       2  16384.0        1+  Paid   0.99       Everyone            Arcade      March 3, 2017         1.0  2.3 and up
10760                                Fast Tract Diet  HEALTH_AND_FITNESS     4.4      35   2457.6    1,000+  Paid   7.99       Everyone  Health & Fitness     August 8, 2018       1.9.3  4.2 and up
10782                        Trine 2: Complete Story                GAME     3.8     252  11264.0   10,000+  Paid  16.99           Teen            Action  February 27, 2015        2.22  5.0 and up
10785                                   sugar, sugar              FAMILY     4.2    1405   9728.0   10,000+  Paid   1.20       Everyone            Puzzle       June 5, 2018         2.7  2.3 and up

[577 rows x 13 columns]

相关问题