0

I tried different stack overflow solutions using pd.read_csv for this file. When I use Excel to text to and use ";" as delimiter in Excel, it gives exactly the output I need.

desired excel output:

data:

'Balance Sheet;"'Package / Number";"Package Type";"Instrument";"Counterparty";"Opening Date";"Value Date";"Maturity Date";"'Nominal Amount";"'Interest Rate";"CCy";"'Funding Type";"Nominal Amount Local";"Interest Rate Local";"'Maturity Year";"'Maturity Quarter";"Tenor";"Tenor Range";"Date Basis"
Asset Finance;"2.915.239";;"IRS-FIX-TO-FLOAT";"X_SEL";"03/27/2019";"03/29/2019";"08/29/2023";"-20.000.000.000";"1
Asset Finance;"2.915.239";;"IRS-FIX-TO-FLOAT";"X_SEL";"03/27/2019";"03/29/2019";"08/29/2023";"20.000.000.000";"2
Asset Finance;;;"IRS-FIX-TO-FLOAT";;"03/27/2019";"03/29/2019";"08/29/2023";;;;"Payer Swap";"20.000.000.000";"-1

Code:

df = pd.read_csv(path2, sep='";"',engine='python')

df = df.apply(lambda x: x.replace('"','')) --\> doesnt seems to be working

The output columns are not split correct. It should be per above column 0: Balance Sheet, 1: Package / Number, 2: 'Package Type etc.. total 19 columns

pandas output:

pandas output

If there is any other work around solutions, pls tell. Thanks!

2
  • Your CSV has issues. Not all columns/values are clearly separated by ";". If you notice the last row in your image, multiple columns are clubbed as first column value, and "Interest Rate" column's value is in "Instrument" column so on. Commented Dec 9, 2022 at 9:12
  • Hi Azhar! Yes indeed. Any thoughts on how to process this data instead? As this data is as-is extracted from system which I can’t change beforehand. Thanks! Commented Dec 10, 2022 at 7:05

1 Answer 1

2

Use only sep=";" to correctly split columns. Add quotechar='"' to tell pandas that " is a quote character and should not be part of value.

df = pd.read_csv(path2,sep=';', quotechar='"',engine='python')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.