【发布时间】:2019-12-02 23:40:57
【问题描述】:
我的行值是
[1,2,3,4,5,6,7,8] 和 column_names ['col1','col2','col3','col4','col5','col6','col7']
如何为 pandas 制作单个数据框,如下所示:
col1 col2 col3 col4 col5 col6 col7
1 2 3 4 5 6 7
【问题讨论】:
我的行值是
[1,2,3,4,5,6,7,8] 和 column_names ['col1','col2','col3','col4','col5','col6','col7']
如何为 pandas 制作单个数据框,如下所示:
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]
【讨论】:
new_df = df[bool_filter]