gpt4 book ai didi

python - Pandas :返回以特定数字开头的列值

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:54 28 4
gpt4 key购买 nike

我有以下 df:

url = 'https://raw.githubusercontent.com/108michael/ms_thesis/master/sic_naics_catcode.csv'
df= pd.read_csv(url, index_col=0)
df.head(3)

SICcode Catcode Category SICname MultSIC 2012 NAICS Code 2002to2007 NAICS
0 111 A1500 Wheat, corn, soybeans and cash grain Wheat X 111140 111140
1 112 A1600 Other commodities (incl rice, peanuts, honey) X 111160 111160
2 115 A1500 Wheat, corn, soybeans and cash grain Corn X 111150 111150

我想返回以 531 或 92 开头的所有行,或者在某些情况下,返回列 2002to2007 NAICS 中以 5416 到 5419 开头的值.

我认为这一定很容易。我熟悉(这只是一个模板)dz = df[(df['date'] > '01/03/2005') & (df['date'] < '01/03/2015')]键入代码,但我不知道是否有任何允许我输入截断值的“通配符”符号。

有什么想法吗?

最佳答案

您可以为此使用 RegEx 功能:

df.loc[df['2002to2007 NAICS'].astype(str).str.contains(r'^(?:531|92|541[6-9])')]

将为您提供所有以 531 或 92 或 5416-5419 开头的值

关于python - Pandas :返回以特定数字开头的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846374/

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