![](/img/trans.png)
[英]How to sort rows values and replace them by column names on a pandas dataframe
[英]Pandas dataframe map rows values with column names
我的行值为[1,2,3,4,5,6,7,8]
和 column_names ['col1','col2','col3','col4','col5','col6','col7']
如何为熊猫制作单个数据框,如下所示:
col1 col2 col3 col4 col5 col6 col7
1 2 3 4 5 6 7
使用嵌套列表:
new_df = pd.DataFrame([[1,2,3,4,5,6,7]],
columns=['col1','col2','col3','col4','col5','col6','col7'])
print (new_df)
col1 col2 col3 col4 col5 col6 col7
0 1 2 3 4 5 6 7
如果您的意思是过滤现有的 df 您可以通过多种方式进行,这是我的建议:
#first you create the auxiliary lists
values = [1,2,3,4,5,6,7,8]
cols = ['col1','col2','col3','col4','col5','col6','col7']
#next, you create a filter for each column
bool_filter = None
for col, value in zip(cols, values):
if is None bool_filter:
bool_filter = df[col] == value
else:
bool_filter = bool_filter & (df[col] == value)
#finnaly apply it to the df
df[bool_filter]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.