我在python
中有3个列表。使用这3个列表,我想创建一个pandas
数据框。我在下面做过。
import pandas as pd
import numpy as np
mysql_list = ['id', 'date', 'name']
oracle_list = ['id', 'date-1', 'name_1']
sql_list = ['id', 'date', 'name-1']
mysql_name='test_123'
oracle_name='test-123'
sql_name='test123'
pd_df = pd.DataFrame(columns = ['mysql_name', 'mysql_cols', 'oracle_name', 'oracle_cols', 'sql_name', 'sql_cols'])
pd_df = pd.DataFrame(index = [np.arange(len(mysql_list))],columns = ['mysql_name', 'mysql_cols', 'oracle_name', 'oracle_cols', 'sql_name', 'sql_cols'])
pd_df.loc[:,'mysql_name'] = mysql_name
pd_df.loc[:,'mysql_cols'] = mysql_list
pd_df.loc[:,'sql_name'] = sql_name
pd_df.loc[:,'sql_cols'] = sql_list
pd_df.loc[:,'oracle_name'] = oracle_name
pd_df.loc[:,'oracle_cols'] = oracle_list
pd_df
mysql_name mysql_cols oracle_name oracle_cols sql_name sql_cols
0 test_123 id test-123 id test123 id
1 test_123 date test-123 date-1 test123 date
2 test_123 name test-123 name_1 test123 name-1
我能够达到我的要求。有一种简单的方法可以用较少的代码行来完成这项工作。
我相信有,但我不知道如何做到这一点
答案 0 :(得分:3)
In [16]: (pd.DataFrame({'mysql_cols':mysql_list, 'oracle_cols':oracle_list,
...: 'sql_cols':sql_list})
...: .assign(mysql_name=mysql_name,oracle_name=oracle_name, sql_name=sql_name)
...: .sort_index(axis=1))
...:
Out[16]:
mysql_cols mysql_name oracle_cols oracle_name sql_cols sql_name
0 id test_123 id test-123 id test123
1 date test_123 date-1 test-123 date test123
2 name test_123 name_1 test-123 name-1 test123
答案 1 :(得分:3)
您可以将assign
与d={'mysql_name':'test_123','oracle_name':'test-123','sql_name':'test123'}
pd.DataFrame({'mysql_cols':mysql_list, 'oracle_cols':oracle_list,
'sql_cols':sql_list}).assign(**d)
Out[312]:
mysql_cols oracle_cols sql_cols mysql_name oracle_name sql_name
0 id id id test_123 test-123 test123
1 date date-1 date test_123 test-123 test123
2 name name_1 name-1 test_123 test-123 test123
/pages?page_id[]=123&page_id[]=542