gpt4 book ai didi

python - 有条件地附加多行数据框

转载 作者:行者123 更新时间:2023-12-04 15:08:15 24 4
gpt4 key购买 nike

我正在迭代一个数据帧,并尝试在第一个数据帧中的一行是特定值时从另一个数据帧中追加新值。

考虑以下两个数据框:

print(full_df)
AccessName PolicyArn
0 arn:aws:glue:sample arn:aws:iam::971340810992:policy/service-role/...
1 arn:aws:glue:sample2 arn:aws:iam::971340810992:policy/service-role/...
2 --- arn:aws:iam::971340810992:policy/service-role/...
3 arn:aws:s3:::sample3 arn:aws:iam::971340810992:policy/service-role/...


print(side_df)
AccessName
0 sample-test
1 query_sample
2 us-east-1-sample

如果full_df中的AccessName为某个值,则在full_df后追加side_df,让所有行的第二行值始终为 arn

arn = 'fixed_value'
for index, row in full_df.iterrows():
if row['AccessName'] == '---':
#Here I don't know how I'd define the code to append the side_df values:
#full_df['AccessName'] = side_df['AccessName']
#full_df['PolicyArn'] = arn

是否最好在此时创建另一个 if 语句并迭代 side_df 值并逐行追加?

这个for函数嵌套在实际代码中,arn是动态生成的。

期望的输出:

                                             AccessName                                          PolicyArn
0 arn:aws:glue:sample arn:aws:iam::971340810992:policy/service-role/...
1 arn:aws:glue:sample2 arn:aws:iam::971340810992:policy/service-role/...
2 --- arn:aws:iam::971340810992:policy/service-role/...
3 sample-test fixed_value
4 query_sample fixed_value
5 us-east-1-sample fixed_value
6 arn:aws:s3:::sample3 arn:aws:iam::971340810992:policy/service-role/...

编写此代码的最佳方式是什么?

最佳答案

首先让我们构建您的side_df:

side_df = pd.DataFrame([['sample-test'], ['query_sample'], ['us-east-1-sample']]
, columns=['AccessName'])
fixed_series = pd.Series(['fixed_value'] * len(side_df), name='PolicyArn').to_frame()
side_df_extended = pd.concat([side_df, fixed_series], axis=1)
print(side_df_extended)

AccessName PolicyArn
0 sample-test fixed_value
1 query_sample fixed_value
2 us-east-1-sample fixed_value

假设full_df如下:

    AccessName              PolicyArn
0 arn:aws:glue:sample arn:aws:iam::971340810992:policy/service-role/
1 arn:aws:glue:sample2 arn:aws:iam::971340810992:policy/service-role/
2 arn:aws:s3:::sample3 arn:aws:iam::971340810992:policy/service-role/
3 arn:aws:glue:sample4 arn:aws:iam::971340810992:policy/service-role/
4 arn:aws:s3:::sample5 arn:aws:iam::971340810992:policy/service-role/

现在让我们获取具有您的条件的行的索引,例如:

indices = full_df['AccessName'] == 'arn:aws:glue:sample2'
rows = full_df[indices].index.tolist()
rows

[1]

现在,您想在条件发生后附加 side_df:

final_df = pd.concat([full_df.iloc[:(rows[0] + 1)], side_df_extended, full_df.iloc[(rows[0] + 1):]], ignore_index=True)
final_df

AccessName PolicyArn
0 arn:aws:glue:sample arn:aws:iam::971340810992:policy/service-role/
1 arn:aws:glue:sample2 arn:aws:iam::971340810992:policy/service-role/
2 sample-test fixed_value
3 query_sample fixed_value
4 us-east-1-sample fixed_value
5 arn:aws:s3:::sample3 arn:aws:iam::971340810992:policy/service-role/
6 arn:aws:glue:sample4 arn:aws:iam::971340810992:policy/service-role/
7 arn:aws:s3:::sample5 arn:aws:iam::971340810992:policy/service-role/

关于python - 有条件地附加多行数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65669321/

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