如何用我的.apply()函数中的新列解决一个讨厌的语法错误

问题描述 投票:0回答:2

我正在尝试重新分类一系列字符串,这些字符串代表应用程序下载的次数,因为它没有显示下载的原始数量。我必须将20个字符串分组为7个不同的项目,并将它们放在名为“downloads”的新列中。

我试过编辑括号和括号。我以前使用过np.apply错误。

from pandas import DataFrame 

fear = pd.read_csv('googleplaystore.csv', encoding='latin')

n_ratings = {'Install':['0+', '1+', '5+', '10+', '50+', '100+', '500+', '1,000+', '5,000+', 
             '10,000+', '50,000+', '100,000+', '500,000+', '1,000,000+', '5,000,000+', '10,000,000+', 
             '50,000,000+', '100,000,000+', '500,000,000+', '1,000,000,000+']}
df = DataFrame(n_ratings, columns=['Install'])  

df['downloads'] = df['Install'].apply(lambda x: '0-1k' if x.isin(['0+', '1+', '5+', '10+', '50+', '100+', '500+'])

df['downloads'] = df['Install'].apply(lambda x: '1k-100k' if x.isin(['1,000+', '5,000+', '10,000+', '50,000+']))

df['downloads'] = df['Install'].apply(lambda x: '100k-1M' if x.isin(['100,000+', '500,000+'])

df['downloads'] = df['Install'].apply(lambda x: '1M-10M' if x.isin(['1,000,000+', '5,000,000+'])

df['downloads'] = df['Install'].apply(lambda x: '10M-100M' if x.isin(['10,000,000+', '50,000,000+'])

df['downloads'] = df['Install'].apply(lambda x: '100M-1B' if x.isin(['100,000,000+', '500,000,000+'])

df['downloads'] = df['Install'].apply(lambda x: '> 1B' if x.isin(['1,000,000,000+'])
python pandas numpy dataframe
2个回答
1
投票

你不需要qazxsw poi或qazxsw poi。只需使用apply,你通过if-else,并根据你通过它的条件np.select

conditions

1
投票

如果你真的想使用apply,你可以定义一个检查一个块中所有条件的函数。

choices
© www.soinside.com 2019 - 2024. All rights reserved.