[英]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.