gpt4 book ai didi

python - Pandas:Drop() int64 基于值返回对象

转载 作者:行者123 更新时间:2023-11-28 18:30:39 32 4
gpt4 key购买 nike

我需要删除一列低于特定值的所有行。我使用了下面的命令,但这会将列作为对象返回。我需要将其保留为 int64:

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

之后我尝试将该字段重新转换为 int64,但这会导致来自完全不同列的数据出现以下错误:

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

最佳答案

我想你需要boolean indexingreset_index :

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

关于python - Pandas:Drop() int64 基于值返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37775308/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com