Updated Solution:
I have data with '|'
separating some of the columns, i.e. it is not strictly csv
. I have imported it as csv and attempting to strip the extra '|'
of the values in specific columns. My data follows:
import pandas as pd
from io import StringIO
dfy = pd.read_csv('Thesis/CRSP/CampaignFin14/pacs14.txt', header=0)
#Replace '|' in cells with series.str methods
for col in dfy:
if dfy[col].dtype == 'object':
dfy[col] = dfy[col].str.replace('|', '')
dfy.head()
|2014| |4111920141231643319| |C00206136| |N00029285| 1000 05/15/2014 \
0 2014 |4021120141205164809| |C00307397| |N00026722| 5000 10/22/2013
1 2014 |4053020141213944220| |C00009985| |N00030676| 4 03/26/2014
2 2014 |4063020141216281752| |C00104299| |N00032088| 1000 05/06/2014
3 2014 |4061920141215566782| |C00164145| |N00034277| 2500 05/22/2014
4 2014 |4102420141226480432| |C00439216| |N00036023| 1000 09/29/2014
For some reason the loop is not taking out the |
The following works but I would like to do all of the columns at once.
dfy['cycle'] = \
dfy['cycle'].map(lambda x: str(x)[:-1])
dfy.head()
cycle cid amount date realcode type di feccandid
0 |2014 |N00029285| 1000 05/15/2014 |E1600| |24K| |D| |H8TX22107|
1 |2014 |N00026722| 5000 10/22/2013 |G4600| |24K| |D| |H4TX28046|
2 |2014 |N00030676| 4 03/26/2014 |C2100| |24Z| |D| |H0MO07113|
This is what my data looks like when I use .csv
, sep=
, to import.
cycle cid amount date realcode type di feccandid
0 |2014| |N00029285| 1000 05/15/2014 |E1600| |24K| |D| |H8TX22107|
1 |2014| |N00026722| 5000 10/22/2013 |G4600| |24K| |D| |H4TX28046|
2 |2014| |N00030676| 4 03/26/2014 |C2100| |24Z| |D| |H0MO07113|
This is what it looks like in .txt
:
|2014|,|4111920141231643319|,|C00206136|,|N00029285|,1000,05/15/2014,|E1600|,|24K|,|D|,|H8TX22107|
|2014|,|4021120141205164809|,|C00307397|,|N00026722|,5000,10/22/2013,|G4600|,|24K|,|D|,|H4TX28046|
|2014|,|4053020141213944220|,|C00009985|,|N00030676|,4,03/26/2014,|C2100|,|24Z|,|D|,|H0MO07113|
|2014|,|4063020141216281752|,|C00104299|,|N00032088|,1000,05/06/2014,|F1100|,|24K|,|D|,|H0OH06189|
|2014|,|4061920141215566782|,|C00164145|,|N00034277|,2500,05/22/2014,|F3100|,|24K|,|D|,|H2NY22139|
Here is a link to my rawdata