GrandConyon
GrandConyon

Reputation: 49

Pandas dataframe map rows values with column names

My row values are [1,2,3,4,5,6,7,8], and column_names ['col1','col2','col3','col4','col5','col6','col7']

How do I make a single dataframe for pandas, like this:

col1 col2 col3 col4 col5 col6 col7
  1   2    3     4    5    6   7 

Upvotes: 1

Views: 1491

Answers (2)

Felício
Felício

Reputation: 163

If you meant filtering an existing df you could do It in many ways, here is my suggestion:

#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] 

Upvotes: 0

jezrael
jezrael

Reputation: 863166

Use nested list:

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

Upvotes: 4

Related Questions