首页 > 解决方案 > 如何使用 pandas/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]]

标签: pythonpandaslist

解决方案


超级简单:

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)

推荐阅读