gpt4 book ai didi

python - 如何过滤以下数据框?

转载 作者:太空宇宙 更新时间:2023-11-04 05:06:34 25 4
gpt4 key购买 nike

你好,我想过滤一个数据框,我希望第一列的所有值都为“0”,问题是 mi csv 文件没有我试过的标题

import pandas as pd

df = pd.read_csv('big-cluster.csv',delimiter=',',encoding='ISO-8859-15')
print(df.iloc[:,0].isin([0]))

但是我得到了:

0       False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 False
11 False
12 False
13 False
14 False
15 False
16 False
17 False
18 False
19 False
20 False
21 False
22 False
23 False

我的 csv 文件具有以下结构:

10,hello this is a test
4,just testing code
3,this is fun
4,you are good
10,this is very good

我想要得到的输出如下:

10,hello this is a test
10,this is very good

根据这里的反馈,我尝试了:

mask = df[0].astype(str).str.contains("0")
print(df[mask])

但是我得到了:

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2392 try:
-> 2393 return self._engine.get_loc(key)
2394 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239)()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359)()

KeyError: 0

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-54-fe5bdde1bff6> in <module>()
65 '''
66
---> 67 mask = df[0].astype(str).str.contains("0")
68 print(df[mask])
69

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
2060 return self._getitem_multilevel(key)
2061 else:
-> 2062 return self._getitem_column(key)
2063
2064 def _getitem_column(self, key):

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
2067 # get column
2068 if self.columns.is_unique:
-> 2069 return self._get_item_cache(key)
2070
2071 # duplicate columns & possible reduce dimensionality

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
1532 res = cache.get(item)
1533 if res is None:
-> 1534 values = self._data.get(item)
1535 res = self._box_item_values(item, values)
1536 cache[item] = res

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
3588
3589 if not isnull(item):
-> 3590 loc = self.items.get_loc(item)
3591 else:
3592 indexer = np.arange(len(self.items))[isnull(self.items)]

/usr/local/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2393 return self._engine.get_loc(key)
2394 except KeyError:
-> 2395 return self._engine.get_loc(self._maybe_cast_indexer(key))
2396
2397 indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239)()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359)()

KeyError: 0

最佳答案

我认为您的问题是您的第一个字段被键入为 int 而不是字符串。因此,让我们使用“astype”和 .str.contains 来检查零。

让我们试试:

mask = df[0].astype(str).str.contains("0")
print(df[mask])

输出:

    0                     1
0 10 hello this is a test
4 10 this is very good

关于python - 如何过滤以下数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312567/

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