'Remove Row Count from Pandas Output
I'm using pandas to ingest a csv file, pull only one column, and output it to a text file. My issue is that when it outputs, it adds a row with the row number. I need that row number to not exist.
So far I've tried the following code: (print will be replaced with file.write(df) when/if I get this working.
import pandas as pd
from pandas import DataFrame
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
file=open('BlockedIPs.txt', 'w')
df = pd.read_csv('IPOutput.csv', dtype='object')
df = df['IP Address (Impacted)'].astype('str')
#df = DataFrame(df) #I've commented these two lines out because they return "None"
#df = df.set_index('IP Address (Impacted)', inplace=True)
print(df)
Actual output is:
... ...
44036 185.176.27.18
44037 51.143.106.177
44038 51.143.106.177
44039 51.143.106.177
44040 173.194.152.170
44041 173.194.152.170
44042 173.194.152.170
44043 173.194.152.123
44044 173.194.152.123
44045 173.194.152.123
Expected output is:
185.176.27.18
51.143.106.177
51.143.106.177
51.143.106.177
173.194.152.170
173.194.152.170
173.194.152.170
173.194.152.123
173.194.152.123
173.194.152.123
Solution 1:[1]
The 'row number' you are referring is an index to the dataframe. If you don't specify an index, Pandas creates one for you automatically. An index, some row identifier, is required for Pandas so you can specify a column of your dataset to be that index, you can create a new one or you can let Pandas do it (default).
If you want to specify a column of your file to be the index, specify the value for the parameter index_col which can be a single column or a list of columns.
See the docs here.
If you want to output the file without an index you can simply set the index parameter of the to_csv function to False (by default it is True)
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 | MichaelD |
