我有以下DataFrame:
customer item1 item2 item3 1 apple milk tomato 2 water orange potato 3 juice mango chips
我想把它翻译成每行的词典列表
rows = [{'customer': 1, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
{'customer': 2, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
{'customer': 3, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]
Run Code Online (Sandbox Code Playgroud) 将列表列表转换为pandas数据帧很容易:
import pandas as pd
df = pd.DataFrame([[1,2,3],[3,4,5]])
Run Code Online (Sandbox Code Playgroud)
但是如何将df变回列表列表呢?
lol = df.what_to_do_now?
print lol
# [[1,2,3],[3,4,5]]
Run Code Online (Sandbox Code Playgroud) 我有一个愚蠢的。细绳:
' Y M D PDSW RSPC NPPC NEE'
Run Code Online (Sandbox Code Playgroud)
字符串中的每个元素对应于 csv 文件中的一列。有没有办法(除了 for 循环)从这个字符串中获取每列的宽度?例如,第一列的宽度为 5('Y'),下一列的宽度为 4('M')...