我有一個 CSV 檔案,其中一列中列出了資料(ID),我通過 for 回圈將擴展名添加到每個 ID,然后我想將 for 回圈中新生成的串列保存到 CSV 檔案中。查看代碼。 CSV 檔案資料
import pandas as pd
# list of extension for the EEG images
channels = ['_Ch_01', '_Ch_02', '_Ch_03', '_Ch_04', '_Ch_05', '_Ch_06', '_Ch_07', '_Ch_08',
'_Ch_09', '_Ch_10', '_Ch_11', '_Ch_12', '_Ch_13', '_Ch_14', '_Ch_15', '_Ch_16', '_Ch_17',
'_Ch_18', '_Ch_19']
# Restrive the ID of images
df = pd.read_csv ("Data.csv")
# Select the EDF column and add an extension to each ID
for ext in channels:
png = df['Data'].tolist()
png_list = list(map(lambda orig_string: orig_string ext ".png", png))
for files in png_list:
if files.endswith(".png"):
print(files)
print(f"want to save all the {files} to csv file in one column")
謝謝!
uj5u.com熱心網友回復:
您甚至不需要為此回圈:
channels = [f"_Ch{i:02}.png" for i in range(1, 20)]
# Cross-product of df['Data'] and channels
# Essentially repeat each row in df['Data'] 19 times, once for each channel
index = pd.MultiIndex.from_product([df['Data'], channels])
# Concatenate the two levels of the index and export
s = index.get_level_values(0) index.get_level_values(1)
s.to_series().to_csv('Output.csv', index=None, header=None)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/414105.html
標籤:
上一篇:如何合并兩個串列,每個串列包含整數和字串,我需要進行計算才能得到一個新串列?
下一篇:如何將嵌套集轉換為串列?
