使用 while 循环过滤 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34957620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:33:21  来源:igfitidea点击:

Filter Pandas DataFrame using a while loop

pythonpandas

提问by hselbie

I'm trying to access filtered versions of a dataframe, using a list with the filter values.

我正在尝试使用带有过滤器值的列表访问数据框的过滤版本。

I'm using a while loop that I thought would plug the appropriate list values into a dataframe filter one by one. This code prints the first one fine but then prints 4 empty dataframes afterwards.

我正在使用一个 while 循环,我认为它可以将适当的列表值一个一个地插入到数据帧过滤器中。此代码打印第一个很好,但之后打印 4 个空数据帧。

I'm sure this is a quick fix but I haven't been able to find it.

我确定这是一个快速修复,但我一直无法找到它。

boatID = [342, 343, 344, 345, 346]
i = 0 
while i < len(boatID):
    df = df[(df['boat_id']==boatID[i])]
    #run some code, i'm printing DF.head to test it works
    print(df.head())
    i = i + 1

Example dataframe:

示例数据框:

   boat_id  activity speed  heading
0      342         1  3.34   270.00
1      343         1  0.02     0.00
2      344         1  0.01   270.00
3      345         1  8.41   293.36
4      346         1  0.03    90.00 

采纳答案by jezrael

I think you overwrite dfby dfin df = df[(df['boat_id']==boatID[i])]:

我认为你覆盖dfdfin df = df[(df['boat_id']==boatID[i])]

Maybe you need change output to new dataframe, e.g. df1:

也许您需要将输出更改为新的数据帧,例如df1

boatID = [342, 343, 344, 345, 346]
i = 0 
while i < len(boatID):
    df1 = df[(df['boat_id']==boatID[i])]
    #run some code, i'm printing DF.head to test it works
    print(df1.head())
    i = i + 1

#   boat_id  activity  speed  heading
#0      342         1   3.34      270
#   boat_id  activity  speed  heading
#1      343         1   0.02        0
#   boat_id  activity  speed  heading
#2      344         1   0.01      270
#   boat_id  activity  speed  heading
#3      345         1   8.41   293.36
#   boat_id  activity  speed  heading
#4      346         1   0.03       90

If you need filter dataframe dfwith column boat_idby list boatIDuse isin:

如果您需要 按列表过滤数据框df,请使用:boat_idboatIDisin

df1 = df[(df['boat_id'].isin(boatID))]
print df1
#   boat_id  activity  speed  heading
#0      342         1   3.34   270.00
#1      343         1   0.02     0.00
#2      344         1   0.01   270.00
#3      345         1   8.41   293.36
#4      346         1   0.03    90.00

EDIT:

编辑:

I think you can use dictionaryof dataframes:

我想你可以使用字典dataframes

print df
   boat_id  activity  speed  heading
0      342         1   3.34   270.00
1      343         1   0.02     0.00
2      344         1   0.01   270.00
3      345         1   8.41   293.36
4      346         1   0.03    90.00

boatID = [342, 343, 344, 345, 346]

dfs = ['df' + str(x) for x in boatID]
dicdf = dict()

print dfs
['df342', 'df343', 'df344', 'df345', 'df346']

i = 0 
while i < len(boatID):
    print dfs[i]
    dicdf[dfs[i]] = df[(df['boat_id']==boatID[i])]
    #run some code, i'm printing DF.head to test it works
#    print(df1.head())
    i = i + 1
print dicdf
{'df344':    boat_id  activity  speed  heading
2      344         1   0.01      270, 'df345':    boat_id  activity  speed  heading
3      345         1   8.41   293.36, 'df346':    boat_id  activity  speed  heading
4      346         1   0.03       90, 'df342':    boat_id  activity  speed  heading
0      342         1   3.34      270, 'df343':    boat_id  activity  speed  heading
1      343         1   0.02        0}

print dicdf['df342']
   boat_id  activity  speed  heading
0      342         1   3.34      270