![](/img/trans.png)
[英]Python - Drop rows from a Pandas DataFrame that contain numbers
[英]Drop all rows that contain any string from a dataframe in Pandas
我有一個 dataframe 包含列中的字符串,這些字符串應該只是浮點數。 我看到了幾個關於如何從單個列中刪除帶有特定字符串或部分字符串的行的解決方案。
所以對於一個單獨的專欄,我想一個人可以這樣做
new_df = df[df['Column'].dtypes != object]
但是這個
new_df = df[df.dtypes != object]
不工作。 可以通過循環遍歷所有列,但是有沒有辦法一次刪除所有列的字符串?
#excluding object columns
new_df = df.select_dtypes(exclude=object)
#only floats columns
new_df = df.select_dtypes(include=float)
#only numeric columns
new_df = df.select_dtypes(include=np.number)
編輯:
new_df = df.apply(pd.to_numeric, errors='coerce').dropna()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.