ohlc = df.values.tolist()
给了我这份清单清单:
[['2018-04-09', 7044.32, 7178.11, 6661.99, 6770.73],
['2018-04-08', 6919.98, 7111.56, 6919.98, 7023.52],
['2018-04-07', 6630.51, 7050.54, 6630.51, 6911.09],
['2018-04-06', 6815.96, 6857.49, 6575.0, 6636.32],
['2018-04-05', 6848.65, 6933.82, 6644.8, 6811.47],...]
对于进一步的分析,我需要将每个列表的Datestring转换为像(2018,04,09)这样的元组
THX
答案 0 :(得分:1)
提示:您可以将字符串拆分为某个分隔符,如此。
>>> s = '2018-04-09'
>>> s.split('-')
['2018', '04', '09']
您也可以通过输入list
将tuple
转换为>>> L = ["a", "b", "c"]
>>> tuple(L)
('a', 'b', 'c')
:
L = tuple(L)
请注意,这些函数是不可变的 - 它们不会更改对象,它们会返回结果(例如,您需要执行{{1}})。
我希望这有助于解决您的问题。
答案 1 :(得分:0)
扩展@TerryA's advice,您可以在pandas
上游进行大部分操作:
lst = [['2018-04-09', 7044.32, 7178.11, 6661.99, 6770.73],
['2018-04-08', 6919.98, 7111.56, 6919.98, 7023.52],
['2018-04-07', 6630.51, 7050.54, 6630.51, 6911.09],
['2018-04-06', 6815.96, 6857.49, 6575.0, 6636.32],
['2018-04-05', 6848.65, 6933.82, 6644.8, 6811.47]]
# we start with a dataframe
df = pd.DataFrame(lst)
# reassign column 0 with list comprehension + conversion to int
df[0] = [tuple(int(i) for i in x.split('-')) for x in df[0]]
# extract list
res = df.values.tolist()
[[(2018, 4, 9), 7044.32, 7178.11, 6661.99, 6770.73],
[(2018, 4, 8), 6919.98, 7111.56, 6919.98, 7023.52],
[(2018, 4, 7), 6630.51, 7050.54, 6630.51, 6911.09],
[(2018, 4, 6), 6815.96, 6857.49, 6575.0, 6636.32],
[(2018, 4, 5), 6848.65, 6933.82, 6644.8, 6811.47]]
答案 2 :(得分:0)
使用list comprehension
并修改列表:
lists = [['2018-04-09', 7044.32, 7178.11, 6661.99, 6770.73],
['2018-04-08', 6919.98, 7111.56, 6919.98, 7023.52],
['2018-04-07', 6630.51, 7050.54, 6630.51, 6911.09],
['2018-04-06', 6815.96, 6857.49, 6575.0, 6636.32],
['2018-04-05', 6848.65, 6933.82, 6644.8, 6811.47]]
lists[:] = [[tuple(item.split('-')) if isinstance(item, str) else item for item in sub]
for sub in lists]
lists
[[('2018', '04', '09'), 7044.32, 7178.11, 6661.99, 6770.73],
[('2018', '04', '08'), 6919.98, 7111.56, 6919.98, 7023.52],
[('2018', '04', '07'), 6630.51, 7050.54, 6630.51, 6911.09],
[('2018', '04', '06'), 6815.96, 6857.49, 6575.0, 6636.32],
[('2018', '04', '05'), 6848.65, 6933.82, 6644.8, 6811.47]]