我有一个看起来像这样的数据框:
col1
file1.csv
file2.csv
file3.csv
...
file500.csv
col1
我需要为每行中的相同位置添加更多字符和特殊字符,而不管所涉及的文件名如何。
为了进行快速测试,我设置了两个变量,一个以原始文件名作为字符串,另一个在其中进行字符串操作,如下所示:
original_string = 'file1.csv'
string_manupilation = 'path.path.\"{}\"'.format(original_string)
的输出string_manupilation
是这样的:
'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\"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句