[英]Select columns of pandas dataframe using a dictionary list value
我在字典中有列名,并且想从数据框中选择那些列。
在下面的示例中,如何选择字典值“ b”,“ c”并将其保存到df1中?
import pandas as pd
ds = {'cols': ['b', 'c']}
d = {'a': [2, 3], 'b': [3, 4], 'c': [4, 5]}
df_in = pd.DataFrame(data=d)
print(ds)
print(df_in)
df_out = df_in[[ds['cols']]]
print(df_out)
TypeError:无法散列的类型:“列表”
删除嵌套列表- []
:
df_out = df_in[ds['cols']]
print(df_out)
b c
0 3 4
1 4 5
根据ref ,只需要放一套括号即可。
df_out = df_in[ds['cols']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.