Transpose Pipe-separated Csv In Python
I have a pipe separated csv file that I need to transpose before loading elsewhere. How to I change the row to columns on each pipe "|"?
So the three lines I want to transpose are in csv like below:
CountryCode|AFG| ALB| DZA CountryISO2|AF| AL| DZ CountryName|Afghanistan| Albania| Algeria
and I am after:
Country Code|CountryISO2|CountryName AFG |AF |Afghanistan ALB |AL |Albania DZA |DZ |Algeria
I've tried using pandas DataFrame and transposing but this takes all the values and loads into a single cell.
df_csv = pd.DataFrame(data=csv) transposed_csv = df_csv.T print(transposed_csv)
ends up like this:
0 1 [CountryCode|AFG| ALB|] [CountryISO2|AF| AL|] [CountryName|Afghanistan| Albania]
It seems that your problem is that the dataframe is not properly loaded. You have to read csv with sep='|' and then you can transpose it.
df_csv = pd.read_csv(data=file.csv, sep='|') df_csv.T 0 1 CountryCode CountryISO2 CountryName AFG AF Afghanistan ALB AL Albania DZA DZ Algeria
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module