'Delete repeating column header in pandas
I am new to python, I am iterating through a list in pandas, but I got the desire result except for it prints the column name each time.
ServerIP HostName Vulnerable_Package
0 10.1 apple tar-(1.28-2.1ubuntu0.2+esm1)
ServerIP HostName Vulnerable_Package
0 10.1 apple zlib1g-(1:1.2.8.dfsg-2ubuntu4.3+esm1)
ServerIP HostName Vulnerable_Package
0 10.1 apple man-db-(2.7.5-1ubuntu0.1~esm1)
I expect the output like below. How to achieve this?
ServerIP HostName Vulnerable_Package
1 10.1 apple tar-(1.28-2.1ubuntu0.2+esm1)
2 10.1 apple zlib1g-(1:1.2.8.dfsg-2ubuntu4.3+esm1)
3 10.1 apple man-db-(2.7.5-1ubuntu0.1~esm1)
I use the code below.
import pandas as pd
for i in range(len(vul_pkgname)):
team = pd.DataFrame(list(zip(serverlist, hostname, vul_pkgname[i])))
team.columns =['ServerIP', 'HostName', 'Vulnerable_Package']
print(team)
Solution 1:[1]
You create and print a new DataFrame for each row, that's why you get multiple header.
Instantiate the DataFrame before the loop, add the headers, and then populate it using append within the loop.
import pandas as pd
team = pd.DataFrame()
team.columns = ['ServerIP', 'HostName', 'Vulnerable_Package']
for i in range(len(vul_pkgname)):
team.append(list(zip(serverlist, hostname, vul_pkgname[i])))
print(team)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NiziL |
