怎么把这些排放到Pandas身上?

2024-04-27 02:42:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图删除pandas数据帧中没有价格的行。从技术上讲,在数据源中,美元符号($)默认显示为 df1.dropna(subset=['Price ($)'], inplace = True)在我的情况下不起作用。如何删除没有附加价格的行。你知道吗

import pandas as pd

first = pd.read_excel('......')
df1 = pd.DataFrame(first)

   Car                          Price ($)

   Honda                          $200
   Benz                           $             ----------> drop
   Chevy                          $300
   BMW                            $250
   Kia                            $             ----------> drop 

Tags: 数据truepandas符号情况价格price数据源
2条回答

如果我理解正确,这应该是可行的:

first = first[first['Price ($)'] != '$']

它只保留Price列不等于'$'的行

另一个答案很好,但这里有另一种方法

df = df[df['Price ($)'].str.contains('\d+')]

这将确保您只获得price列中有数字价格的行。作为奖励,这也更快:)

相关问题 更多 >