Python Forum
Sorting data with pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting data with pandas
#5
You should show how you are importing the data to help answer the question.

I would recommend parsing this data into an actual csv format before reading into pandas:

import csv


headers = ['AN', 'Artikel']
with open('data.txt', 'r') as in_data, open('out.csv', 'w', newline='') as out_csv:
    csv_writer = csv.writer(out_csv)

    i = 0
    row = []

    csv_writer.writerow(headers)

    # make sure your data ends in a new line or you won't import the last line
    for line in in_data.readlines():
        if i % 2 == 0 and i != 0:
            csv_writer.writerow(row)
            row.clear()
        
        i += 1
        row.append(line.strip())

    csv_writer.writerow(row)
    row.clear()
This converts this:
12345S
Item 1
4152DS
Item 2
15190A
Item 3


To this:

AN,Artikel
12345S,Item 1
4152DS,Item 2
15190A,Item 3


Which will be read by pandas cleaner.You should show how you are importing the data to help answer the question.

Which will be read by pandas cleaner.

Edit: To import and sort:

import pandas as pd
df = pd.read_csv('out.csv')
df.sort_values(["AN"], inplace=True)
print(df.head())
Output:
       AN Artikel
0  12345S  Item 1
2  15190A  Item 3
1  4152DS  Item 2
Reply


Messages In This Thread
Sorting data with pandas - by TheZaind - Nov-15-2021, 06:58 PM
RE: Sorting data with pandas - by jefsummers - Nov-15-2021, 07:25 PM
RE: Sorting data with pandas - by TheZaind - Nov-15-2021, 07:40 PM
RE: Sorting data with pandas - by jefsummers - Nov-17-2021, 12:11 PM
RE: Sorting data with pandas - by aserian - Nov-22-2021, 07:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Data frame column condition check based on length of the value aditi06 1 812 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  [Pandas] Write data to Excel with dot decimals manonB 1 1,030 May-05-2021, 05:28 PM
Last Post: ibreeden
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 1,087 Feb-20-2021, 08:25 PM
Last Post: perfringo
  pandas read_csv can't handle missing data mrdominikku 0 1,117 Jul-09-2020, 12:26 PM
Last Post: mrdominikku
  Pandas data frame creation from Kafka Topic vboppa 0 841 Jul-01-2020, 04:23 PM
Last Post: vboppa
  Generate Test data (.csv) using Pandas Ashley 5 1,388 Jun-15-2020, 02:51 PM
Last Post: jefsummers
  Read json array data by pandas vipinct 0 894 Apr-13-2020, 02:24 PM
Last Post: vipinct
  add formatted column to pandas data frame alkaline3 0 819 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  pandas DataReader error on all data sources glidecode 5 14,225 Sep-25-2019, 02:10 PM
Last Post: perfringo
  Loop pandas data frame by position ? Johnse 1 1,235 Sep-06-2019, 12:26 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020