我有以下 csv 文件在 pands 中读取为 df
table entity_name node_name src_name table_col_name look_up_indicator type keys
gw_policy account ns0 fullname insured_name N attribute NA
gw_policy polocy ns1 agent_name agent N attribute NA
gw_policy account ns2 phone_num agent_phone N attribute NA
我将csv读入pandas并提取特定的列值
在这种情况下,我只从“table_col_name”中提取“agent”行
data = pd.read_csv(file_path)
policy=data.loc[data['table_col_name']=='agent', ['node_name', 'src_name']]
print(policy)
它会将数据框打印为
node_name src_name
ns1 agent_name
现在我想遍历列循环
column_name=['insured_name', 'agent', 'agent_phone']
for value in column_name:
policy=data.loc[data['table_col_name']==value, ['node_name', 'src_name']]
print("Looped attribute",policy)
#Output
Looped attribute node_name src_name
table
gw_policy ns0 fullname
Looped attribute node_name src_name
table
gw_policy ns1 agent_name
Looped attribute node_name src_name
table
gw_policy ns2 phone_num
#预期: 我只需要获取这样的行值
ns0 fullname
ns1 agent_name
ns2 phone_num
感谢是否有人可以对此提供帮助?
谢谢
答案 0 :(得分:0)
从@ jezrael 得到答案。方法是让列表脱离过滤器
data.loc[data['table_col_name']==value, ['node_name', 'src_name']].iloc[0].tolist()
谢谢