我有一个大约20列的熊猫数据框。替换熊猫数据框中所有出现的字符串(Python)
有可能通过手动编写所有列名替换字符串(这里换行符)的所有事件:
df['columnname1'] = df['columnname1'].str.replace("\n","<br>")
df['columnname2'] = df['columnname2'].str.replace("\n","<br>")
df['columnname3'] = df['columnname3'].str.replace("\n","<br>")
...
df['columnname20'] = df['columnname20'].str.replace("\n","<br>")
这不幸的是不起作用:
df = df.replace("\n","<br>")
是否有任何其他,更优雅的解决方案?
这并没有为我工作!熊猫版本'0.15.1',python 2.7.9,Ubuntu 14.04。 – 2015-04-06 02:55:00
'Python 2.7.9 | Anaconda 2.1.0(64-bit)| (default,2015年3月9日,16:20:48) [GCC 4.4.7 20120313(Red Hat 4.4.7-1)] on linux2 输入“help”,“copyright”,“credits”或“license”更多信息。 Anaconda由Continuum Analytics提供。 请查看:http://continuum.io/thanks和https://binstar.org >>> import pandas as pd >>> df = pd.DataFrame({'a':['1 \ n '','2','3'],'b':['4 \ n','5','6 \ n']}) >>> df ab 0 1 \ n 4 \ ñ 1 2 \ N 5 2 3 6 \ n >>> df.replace({ '\ n': '
'}) AB 0 1 \ N 4 \ n 1 2 \ N 5 2 3 6 \ n >>> ' – 2015-04-06 02:55:28
感谢您指出这一点@YichuanWang。 – 2015-04-11 11:57:15