Reputation: 27
I have a CSV file with data (IDs) listed in one column and I added the extension to each ID via a for loop and then I would like to save the newly generated list in for loop to a CSV file. See the code. CSV file Data
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")
Thanks!
Upvotes: 1
Views: 789
Reputation: 93181
You don't even need looping for this:
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)
Upvotes: 1