如何使用pandas/python将变量值放入列表的其他列表中

2024-09-26 17:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个变量:

date = 2018-10-25

所有日期都存储在

df =[['Euro', 0.8762059999999999], ['British Pound', 0.7755920000000001], ['Indian Rupee', 73.246211], ['Australian Dollar', 1.4093959999999999], ['Canadian Dollar', 1.308288], ['Singapore Dollar', 1.379124], ['Swiss Franc', 0.999036], ['Malaysian Ringgit', 4.1631849999999995], ['Japanese Yen', 112.293159], ['Chinese Yuan Renminbi', 6.944638]]

就像那张单子一样。你知道吗

我想要输出:[['Euro',2018-10-25, 0.8762059999999999],['British Pound', 2018-10-25, 0.7755920000000001],['Indian Rupee',2018-10-25, 73.246211],....]对于使用pandas/python的for循环的列表的所有元素都是这样的。你知道吗

想把它存储在Mysql数据库中,那么,它的查询是如何产生的呢?你知道吗

所以请帮助指导我该怎么做。 我试过这个但没用:

 total = []
 for i in df:
         total = [df[0][0], date, df[0][1]]

Tags: dffordatetotalindianrupeedollarswiss
2条回答

试试这个:

date = '2018-10-25'
for i in df:
    i.insert(1,date)

In [1154]: df
Out[1154]: 
[['Euro', '2018-10-25', 0.8762059999999999],
 ['British Pound', '2018-10-25', 0.7755920000000001],
 ['Indian Rupee', '2018-10-25', 73.246211],
 ['Australian Dollar', '2018-10-25', 1.4093959999999999],
 ['Canadian Dollar', '2018-10-25', 1.308288],
 ['Singapore Dollar', '2018-10-25', 1.379124],
 ['Swiss Franc', '2018-10-25', 0.999036],
 ['Malaysian Ringgit', '2018-10-25', 4.1631849999999995],
 ['Japanese Yen', '2018-10-25', 112.293159],
 ['Chinese Yuan Renminbi', '2018-10-25', 6.944638]]

现在,您可以从上面创建一个dataframe插入到Mysql

frame = pd.DataFrame(df)
frame.columns = ['Currency' ,'date', 'value']
frame.date = frame.date.apply(pd.to_datetime)
In [1156]: frame
Out[1156]: 
                       0           1           2
0                   Euro  2018-10-25    0.876206
1          British Pound  2018-10-25    0.775592
2           Indian Rupee  2018-10-25   73.246211
3      Australian Dollar  2018-10-25    1.409396
4        Canadian Dollar  2018-10-25    1.308288
5       Singapore Dollar  2018-10-25    1.379124
6            Swiss Franc  2018-10-25    0.999036
7      Malaysian Ringgit  2018-10-25    4.163185
8           Japanese Yen  2018-10-25  112.293159
9  Chinese Yuan Renminbi  2018-10-25    6.944638

from pandas.io import sql
import MySQLdb

frame.to_sql(con=con, name='table_name', if_exists='replace', flavor='mysql', index=False)

让我知道它是否有效。你知道吗

超级简单:

date = "2018-10-25"

df =[['Euro', 0.8762059999999999], ['British Pound', 0.7755920000000001], ['Indian Rupee', 73.246211], ['Australian Dollar', 1.4093959999999999], ['Canadian Dollar', 1.308288], ['Singapore Dollar', 1.379124], ['Swiss Franc', 0.999036], ['Malaysian Ringgit', 4.1631849999999995], ['Japanese Yen', 112.293159], ['Chinese Yuan Renminbi', 6.944638]]

// Loop through df, i being the position, and append the date to the end of each
// of those arrays under df, because df is a multi dimensional array.
for i in df:
   i.insert(1,date)

print(df)

相关问题 更多 >

    热门问题