1

Here is a short sample of my txt file, county-covid-data.txt

#County,Confirmed Cases,Probable Cases,Deaths
Anderson,4595,1137,85
Andrews,1630,0,37
Angelina,4169,2688,208
Aransas,697,245,29
Archer,650,90,10
Armstrong,97,38,6
Atascosa,3267,930,96
Austin,1322,190,22
Bailey,561,200,16
Bandera,710,194,21
Bastrop,3536,1221,59
Baylor,111,172,11
Bee,2798,397,55
Bell,17752,0,238
Bexar,135648,26460,2181
Blanco,279,55,12
Borden,12,6,0
Bosque,836,189,22
Bowie,3653,1478,150

When reading the text file into my df:

import pandas
import pandas as pd
cov = pandas.read_table('county-covid-data.txt', delim_whitespace=True, names=('County','Confirmed Cases', 'Probable Case', 'Deaths'))

and i get all of the counties, confirmed and probable cases, and deaths in one column, and the top row in three different cells that seemed to separate by the spaces

How do I remove the commas and the pound sign before county while also separating each value into the corresponding column? Ive never read a txt file before, and everything I've tried so far looks like this or a a block of text.

sarsmth23
  • 13
  • 2

1 Answers1

0

Use read_csv() whose default delimiter is ,.

import pandas as pd

cov = pd.read_csv('county-covid-data.txt')

Or designate sep or delimiter of read_table.

cov = pd.read_table('county-covid-data.txt', sep=',', names=('County','Confirmed Cases', 'Probable Case', 'Deaths'), skiprows=1)

To remove pound in column header, you can use

cov.columns = cov.columns.str.strip('#')
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52