如何从我的数据帧输出中删除\ n?

时间:2019-12-05 02:11:35

标签: pandas

enter image description here

我的代码如下: 我正在尝试学习数据科学,并在我的输出中发现了这个\ n。 有人可以帮忙解决吗?

年份\ n团队\ n GP \ n GS \ n MPG \ n FG%\ n 3P%\ n FT%\ n RPG \ n APG \ n SPG \ n BPG \ n PPG \ n

0 1984–85 \ n芝加哥\ n 82 82 38.3 .515 .173 .845 6.5 5.9 2.4 .8 28.2 \ n

1 1985–86 \ n芝加哥\ n 18 7 25.1 .457 .167 .840 3.6 2.9 2.1 1.2 22.7 \ n

2 1986–87 \ n芝加哥\ n 82 82 40.0 .482 .182 .857 5.2 4.6 2.9 1.5 37.1 * \ n

3 1987–88 \ n芝加哥\ n 82 82 40.4 * .535 .132 .841 5.5 5.9 3.2 * 1.6 35.0 * \ n

4 1988–89 \ n芝加哥\ n 81 81 40.2 * .538 .276 .850 8.0 8.0 2.9 .8 32.5 * \ n

5 1989–90 \ n芝加哥\ n 82 82 39.0 .526 .376 .848 6.9 6.3 2.8 * .7 33.6 * \ n

6 1990–91†\ n芝加哥\ n 82 82 37.0 .539 .312 .851 6.0 5.5 2.7 1.0 31.5 * \ n

7 1991–92†\ n芝加哥\ n 80 80 38.8 .519 .270 .832 6.4 6.1 2.3 .9 30.1 * \ n

8 1992–93†\ n芝加哥\ n 78 78 39.3 .495 .352 .837 6.7 5.5 2.8 * .8 32.6 * \ n

9 1994–95 \ n芝加哥\ n 17 17 39.3 .411 .500 .801 6.9 5.3 1.8 .8 26.9 \ n

10 1995–96†\ n芝加哥\ n 82 82 37.7 .495 .427 .834 6.6 4.3 2.2 .5 30.4 * \ n

11 1996–97†\ n芝加哥\ n 82 82 37.9 .486 .374 .833 5.9 4.3 1.7 .5 29.6 * \ n

12 1997–98†\ n芝加哥\ n 82 82 38.8 .465 .238 .784 5.8 3.5 1.7 .5 28.7 * \ n

13 2001-02 \ n华盛顿\ n 60 53 34.9 .416 .189 .790 5.7 5.2 1.4 .4 22.9 \ n

14 2002-03 \ n华盛顿\ n 82 67 37.0 .445 .291 .821 6.1 3.8 1.5 .5 20.0 \ n

15职业生涯\ n 1,072 1,039 38.3 .497 .327 .835 6.2 5.3 2.3 .8 30.1 \ n无

16个全明星\ n 13 13 29.4 .472 .273 .750 4.7 4.2 2.8 .5 20.2 \ n无

response = requests.get(links)

soup = BeautifulSoup(response.text,'html.parser')
table = soup.find('table', class_ = 'wikitable sortable')
all_raws = table.find_all('tr')
data = []
for raw in all_raws:
    raw_list = raw.find_all('td')                
//raw_list will print only last raw of table 

[<td colspan="2" style="text-align:center;">All-Star </td>, <td>13</td>, <td>13</td>, <td>29.4</td>, <td>.472</td>, <td>.273</td>, <td>.750</td>, <td>4.7</td>, <td>4.2</td>, <td>2.8</td>, <td>.5</td>, <td>20.2</td>]

    dataRaw = []
    for cell in raw_list:
        dataRaw.append(cell.text)                # datRaw will print only last raw  ['All-Star\n', '13', '13', '29.4', '.472', '.273', '.750', '4.7', '4.2', '2.8', '.5', '20.2\n']

    data.append(dataRaw)

data = data[1:]


header_list = []
col_header = table.find_all('th')
for col in col_header:
    header_list.append(col.text)

df = pd.DataFrame(data)
df.columns = header_list
df

1 个答案:

答案 0 :(得分:0)

这应该有效

df['<column name>'] = df.<column name>.str.replace(r'\n', '')