簡體   English   中英

Pandas:Drop()int64基於值返回對象

[英]Pandas: Drop() int64 based on value returns object

我需要刪除一列低於某個值的所有行。 我使用下面的命令,但這會將列作為對象返回。 我需要將其保留為int64

df["customer_id"] = df.drop(df["customer_id"][df["customer_id"] < 9999999].index)
df = df.dropna()

我曾嘗試將字段重新int64int64 ,但是這會導致以下錯誤來自完全不同的列中的數據:

invalid literal for long() with base 10: '2014/03/09 11:12:27'

我認為你需要使用reset_index boolean indexing

import pandas as pd

df = pd.DataFrame({'a': ['s', 'd', 'f', 'g'],
                'customer_id':[99999990, 99999997, 1000, 8888]})
print (df) 
   a  customer_id
0  s     99999990
1  d     99999997
2  f         1000
3  g         8888

df1 = df[df["customer_id"] > 9999999].reset_index(drop=True)
print (df1)
   a  customer_id
0  s     99999990
1  d     99999997

drop解決方案,但速度較慢:

df2 = (df.drop(df.loc[df["customer_id"] < 9999999, 'customer_id'].index))
print (df2)
   a  customer_id
0  s     99999990
1  d     99999997

時間

In [12]: %timeit df[df["customer_id"] > 9999999].reset_index(drop=True)
1000 loops, best of 3: 676 µs per loop

In [13]: %timeit (df.drop(df.loc[df["customer_id"] < 9999999, 'customer_id'].index))
1000 loops, best of 3: 921 µs per loop

切片整個框架有什么問題(必要時重新索引)?

df = df[df["customer_id"] < 9999999]
df.index = range(0,len(df))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM