0

I am running python program which takes csv data as string, need to sort it and return output as string

input= "Bet,Charle,Daniell,Ada,Eri\n17945,10091,10088,3907,10132\n2,12,13,48,11"

desired output = "Ada,Bet,Charle,Daniell,Eri\n3907,17945,10091,10088,10132\n48,2,12,13,11"

import pandas as pd
from io import StringIO

def sort_csv(input):
  str_data = StringIO(input)
  output_df = pd.read_csv(str_data, sep=",")
  output_df = output_df.sort_index(axis=1)
  output_df.to_csv(path_or_buf='temp.csv', index=False)
  data = open('temp.csv').read()
  return data

I am facing below error TypeError: slice indices must be integers or None or have an index method

pandas package is upgraded, using python 3.4. Any help?

5
  • 2
    May I ask why are you even using pandas for this? it's a huge overkill Commented Mar 14, 2021 at 18:35
  • How are you calling the sort_csv function on your string? Asking because when using sort_csv(input), your code actually works for me. Commented Mar 14, 2021 at 18:37
  • Any better idea instead of using pandas?, as pandas has good functions for sorting and converting, I have opted for pandas Commented Mar 14, 2021 at 18:39
  • Yes @sigma1510, now its working fine for me in pycharm IDE with python 3.7.2,, but not working for python 3.4 with plain python run . Also I am not even slicing the dataframe. Was curious about the error. Also looking for better way instead of pandas(as deepspace mentioned seems its overkill) Commented Mar 14, 2021 at 18:47
  • If you type python3 --version into the terminal, what version do you get? Sometimes when python2 and python3 are both installed, the terminal will default to python2. Commented Mar 14, 2021 at 18:52

1 Answer 1

2

Here is an implementation that avoids using pandas, and allows for the ability to sort by different columns. Once you break your input string into a list of lists, your specified sorting column (i.e. the list you want to use to sort from your list of lists) is sorted whilst keeping note of the original indices, which are then passed as the sorting key to the other lists. Afterwards, you simply re-join the string together:

x =  "Bet,Charle,Daniell,Ada,Eri\n17945,10091,10088,3907,10132\n2,12,13,48,11"

col_to_sort = 0

sublists = [i.split(',') for i in x.split()]

sorted_list = [[l[i] for i in sorted(range(len(sublists[col_to_sort])), key=lambda k: sublists[col_to_sort][k])] for l in sublists]

print(repr('\n'.join(','.join(k) for k in sorted_list)))

Yields:

'Ada,Bet,Charle,Daniell,Eri\n3907,17945,10091,10088,10132\n48,2,12,13,11'
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.