在尷尬地未能自己想到快速解決方案后,我被迫在這里提問。所以我有以下幾點:
keys= ["car", "cat", "sky"]
value_a = ['red', 'furry','cloudy']
value_b = ['blue', 'puffy','rainy']
value_c = ['yellow', 'pet','clear']
all_data = [value_a ,value_b,value_c]
def csv_exporter(keys, all_data):
dictionary = dict(zip(keys, all_data))
df = pd.DataFrame(dictionary)
print(df)
csv_exporter(keys, all_data)
所以我的輸出是:
car cat sky
0 red blue yellow
1 furry puffy pet
2 cloudy rainy clear
但我想要的是這個
car cat sky
0 red furry cloudy
1 blue puffy rainy
2 yellow pet clear
另外,我首先創建一個字典,然后匯出為資料框,因為這對我的應用程式更有效。
uj5u.com熱心網友回復:
你也需要壓縮all_data:
def csv_exporter(keys, all_data):
dictionary = dict(zip(keys, zip(*all_data)))
df = pd.DataFrame(dictionary)
print(df)
結果:
>>> csv_exporter(keys, all_data)
car cat sky
0 red furry cloudy
1 blue puffy rainy
2 yellow pet clear
uj5u.com熱心網友回復:
你試過簡單的變換嗎?
all_data_new=[]
for element in range(len(all_data[0])):
value_new=[]
for dataSet in range(len(all_data)):
value_new.append(all_data[dataSet][element])
all_data_new.append(value_new)
all_data_new >> [['red', 'blue', 'yellow'], ['furry', 'puffy', 'pet'], ['cloudy', 'rainy', 'clear']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352451.html
上一篇:在Python3中使用filter()從字典值串列中洗掉串列
下一篇:python中兩個串列的乘積
