如何使用pandas从字典中删除行

2024-03-29 01:05:50 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个像这样的数据集

date    PCP1    PCP2    PCP3    PCP4
1/1/1985    0   -99 -99 -99
1/2/1985    0   -99 -99 -99
1/3/1985    0   0   -99 -99
1/4/1985    0   0   -99 -99
1/5/1985    1   -99 1   1
1/6/1985    0   -99 -99 -99
1/7/1985    0   1   -99 0
1/8/1985    0   2   -99 3
1/9/1985    0   -99 -99 -99

我想通过只包含date列和一个PCP列来创建新的数据帧,如下所示。。对于df1

df1 = 
date    PCP1
1/1/1985    0
1/2/1985    0
1/3/1985    0
1/4/1985    0
1/5/1985    1
1/6/1985    0
1/7/1985    0
1/8/1985    0
1/9/1985    0

和df2

df2 = 
date    PCP2
1/1/1985    -99
1/2/1985    -99
1/3/1985    0
1/4/1985    0
1/5/1985    -99
1/6/1985    -99
1/7/1985    1
1/8/1985    2
1/9/1985    -99

等等,对于df3。。和df4

我想删除每个数据帧的-99行,这将导致

df1 = 
date    PCP1
1/1/1985    0
1/2/1985    0
1/3/1985    0
1/4/1985    0
1/5/1985    1
1/6/1985    0
1/7/1985    0
1/8/1985    0
1/9/1985    0

和df2

df2 = 
date    PCP2
1/3/1985    0
1/4/1985    0
1/7/1985    1
1/8/1985    2

我不确定是否正确,但我已经编写了以下代码,但我不确定在执行for循环时如何使用-99删除行

# first I created a list of pcp list
n_cols = 4
pcp_list = []
df_names = []
for i in range(1,n_cols):
    item = "PCP" + str(i)
    pcp_list.append(item)
    item_df = "df" + str(i)
    df_names.append(item_df)

# and then I have created a new df for each name on the list by creating a dict
dfs ={}
for dfn, name in zip(df_names, pcp_list):
    dfs[dfn] = pd.DataFrame(df, columns=['date', name])

# and then I was hoping I could remove the rows with -99
for df, name in zip(dfs, pcp_list):
    df[name] = dfs[df[name] = -99]

任何帮助都将不胜感激

谢谢大家!


1条回答
网友
1楼 · 发布于 2024-03-29 01:05:50

您可以在词典中创建一致理解数据帧:

d = {k: v[v != -99].reset_index() for k,v in df.set_index('date').to_dict('series').items()}

按名称创建变量不是recommended,但可以:

for i, (k, v) in enumerate(df.set_index('date').to_dict('series').items()):
    globals()[f'df{i}'] =  v[v != -99].reset_index()

相关问题 更多 >