Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Sorting columns from text file help
#1
Hello! I'm very new to Python and need help figuring out how to sort by columns from a text file which is being throw off by the Date column (which has single white spaces, so trying to separate by tabs is not working).

Text file (sample) contents is below (actual text file is several thousand lines long).

Date           	   		Num1    Num2   
Sat. Aug 10, 2019              41          59          
Wed. Aug 07, 2019              47          8              
Sat. Aug 03, 2019               45          68  



The CODE I tried:

df = pd.read_csv('columns_test.txt', names=['Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df
The OUTPUT I get is:

Date	Num1	Num2
Sat.	Aug	10,	2019	41	59
Wed.	Aug	07,	2019	47	8
Sat.	Aug	03,	2019	45	68
I have also tried other parameters such as sep='\t' , delim_whitespace=True , etc. But it's the Date column with the single white spaces between that is throwing off the columns being separated under the right heading. My goal is to import the data from the text file for data analysis but they need to be sorted under the right columns.

Any advice on how I can achieve this would be greatly appreciated!
Quote
#2
(Nov-15-2019, 05:10 PM)nickglobal101 Wrote: The CODE I tried:

df = pd.read_csv('columns_test.txt', names=['Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df

Hi!

Have you tried this?:

df = pd.read_csv('columns_test.txt', names=['Weekday','Date','Num1','Num2'], skiprows=1, sep='\s+', engine='python')
df
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Rename file from value in text file Nuge93 1 122 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  Trying to make column based file from text file scor1pion 7 473 Jul-16-2019, 02:43 PM
Last Post: scor1pion
  How best to format text in a simple text file? Pedroski55 2 753 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 440 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  Sorting csv or txt file on date and time AshBax 4 1,988 Mar-27-2019, 02:02 PM
Last Post: AshBax
  write each line of a text file into separate text files and save with different names Shameendra 3 502 Feb-20-2019, 09:07 AM
Last Post: buran
  How do i read particular text from text file and update those values in MS SQL table ganeshsai2912 33 3,646 Nov-20-2018, 12:55 AM
Last Post: ganeshsai2912
  Fuzzy match on text columns within dataframe Nsaibot 0 1,314 Aug-27-2018, 10:52 PM
Last Post: Nsaibot
  How to write a code to open and read text file by clicking on the "text file" kavindu 4 1,040 Jul-06-2018, 06:55 PM
Last Post: buran
  How to removewhat is common from one text file using another text file petertyler 1 684 May-25-2018, 12:28 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)