Python 3.x 如何将字符串和特殊字符添加到新列中dataframe中的每一行?

Python 3.x 如何将字符串和特殊字符添加到新列中dataframe中的每一行?,python-3.x,string,pandas,Python 3.x,String,Pandas,我有一个如下所示的数据帧: col1 file1.csv file2.csv file3.csv ... file500.csv 对于col1中的每一行,无论文件名是什么,我都需要在完全相同的位置添加更多字符和特殊字符 为了进行快速测试,我设置了两个变量,一个是原始文件名为字符串的变量,另一个是执行字符串操作的变量,如下所示: original_string = 'file1.csv' string_manupilation = 'path.path.\"{}\"'.format(orig

我有一个如下所示的数据帧:

col1
file1.csv
file2.csv
file3.csv
...
file500.csv
对于
col1
中的每一行,无论文件名是什么,我都需要在完全相同的位置添加更多字符和特殊字符

为了进行快速测试,我设置了两个变量,一个是原始文件名为字符串的变量,另一个是执行字符串操作的变量,如下所示:

original_string = 'file1.csv'


string_manupilation = 'path.path.\"{}\"'.format(original_string)
string\u manufilation
的输出如下:

'path.path."file.csv"'
我需要它看起来像这样,为什么要删除斜线

'path.path.\"file.csv\"'
如果字符串操作变量成功,我可以滚动它们以迭代整个数据帧:

对于索引,df.iterrows()中的行:
[字符串操作]

这应该可以满足您的需要:

操纵

df = pd.DataFrame(['file1.csv','file2.csv','file3.csv'], columns = ['col1'])
df['string_manipulation'] = df['col1'].map(lambda x: 'path.path.\\"{}\\"'.format(x))
输出

print(df.to_string())
        col1      string_manipulation
0  file1.csv  path.path.\"file1.csv\"
1  file2.csv  path.path.\"file2.csv\"
2  file3.csv  path.path.\"file3.csv\"

只需执行
df['col1']='path.path.+df['col1']
?@QuangHoang我不能这样做,因为我需要在特定位置使用反斜杠
'path.path.\\\'+df['col1']+'\\\'
?@QuangHoang这很接近,但两个双引号需要保持我在这里的方式
'path.path.\'file.csv\'