我正在使用Pandas读取csv,然后在各个列中进行一些编辑。当覆盖/保存原始csv时,第一次编辑(用'。'替换NaN值)可以正常工作。但是,第二次编辑(用“ stim_file”列中的“ .VAS_Presented.jpg”替换“。”)是有问题的。在这种情况下,当我在编辑后尝试保存时,仅将列“ stim_file”保存到我的原始csv中。也就是说,在对“ stim_file”列进行编辑之后,我最终的csv文件就是“ stim_file”列,而不是整个带有经过编辑的列的内容。
我如何在“ stim_file”列中进行编辑,然后仅覆盖/保存对该列的编辑?
谢谢。
# TSV CUE Files
tsv_CUE1 = glob.glob(os.path.join(final_func_path, '*cue_run-01_events*'))[0]
tsv_CUE2 = glob.glob(os.path.join(final_func_path, '*cue_run-02_events*'))[0]
# Read tsv files, edit, then copy to original. First missing data in last column, then VAS_Presented.jpg issue
# CUE 1--deal with missing info in last column
read_CUE1 = pd.read_csv(tsv_CUE1, delimiter='\t') # Use pandas to read CUE1 tsv file
new_CUE1 = read_CUE1.fillna('.') # Replace NaN values with '.' and save as new file
new_CUE1.to_csv(tsv_CUE1, index=False, sep='\t') #index=False--> do not print row index #s from dataframe. # Print new_CUE1 to / overwrite tsv file
# CUE2--deal with missing info in last column
read_CUE2 = pd.read_csv(tsv_CUE2, delimiter='\t') # Use pandas to read CUE1 tsv file
new_CUE2 = read_CUE2.fillna('.') # Replace NaN values with '.' and save as new file
new_CUE2.to_csv(tsv_CUE2, index=False, sep='\t') #index=False--> do not print row index #s from dataframe. # Print new_CUE1 to / overwrite tsv file
# CUE1--deal with 'stim_file' column ('.' --> 'VAS_Presented.jpg')
read_CUE1_stim_col = pd.read_csv(tsv_CUE1, delimiter='\t')
new_CUE1_stim_col = read_CUE1_stim_col['stim_file'].replace(['.'],'VAS_Presented.jpg')
new_CUE1_stim_col.to_csv(tsv_CUE1, index=False, sep='\t')
# CUE2--deal with 'stim_file' column ('.' --> 'VAS_Presented.jpg')
read_CUE2_stim_col = pd.read_csv(tsv_CUE2, delimiter='\t')
new_CUE2_stim_col = read_CUE1_stim_col['stim_file'].replace(['.'],'VAS_Presented.jpg')
new_CUE2_stim_col.to_csv(tsv_CUE2, index=False, sep='\t')