1

I have a info.txt file it looks like this:

B 19960331 00100000 00000000000000 00000000000000 00000000000000 00000000 00000000000000 00000000000000 00000000000000
B 19960430 00099100 00000000000000 00000000000000 00000000000000 00000000 00000000000000 00000000000000 00000000000000
B 19960531 00098500 00000000000000 00000000000000 00000000000000 00000000 00000000000000 00000000000000 00000000000000

And when I use pandas to read it:

import pandas as pd
import numpy as np
df =pd.read_csv('C:\Users\Petter\Desktop\info.txt')
df

and the output is:

    B 19960331 00100000 00000000000000 00000000000000 00000000000000 00000000 00000000000000 00000000000000 00000000000000
                                                                     0  B 19960430 00099100 00000000000000 00000000000...
                                                                     1  B 19960531 00098500 00000000000000 00000000000...

The rows are far right from the column name,is there anyway I can make them like this:

    B 19960331 00100000 00000000000000 00000000000000 00000000000000 00000000 00000000000000 00000000000000 00000000000000
0   B 19960430 00099100 00000000000000 00000000000...
1   B 19960531 00098500 00000000000000 00000000000...

I tried:

df.shift(periods=1, axis="columns")

but now work

William
  • 3,724
  • 9
  • 43
  • 76

1 Answers1

2

You can try to read the file with pd.read_csv:

df = pd.read_csv("your_file.txt", sep=r"\s", header=None, dtype=str, engine="python")
print(df)

Prints:

   0         1         2               3               4               5         6               7               8               9
0  B  19960331  00100000  00000000000000  00000000000000  00000000000000  00000000  00000000000000  00000000000000  00000000000000
1  B  19960430  00099100  00000000000000  00000000000000  00000000000000  00000000  00000000000000  00000000000000  00000000000000
2  B  19960531  00098500  00000000000000  00000000000000  00000000000000  00000000  00000000000000  00000000000000  00000000000000
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Thank you so much for your help sir, can you help me with this one https://stackoverflow.com/questions/68088484/how-to-automatically-fill-blank-column-with-none-in-pandas – William Jun 22 '21 at 17:44
  • Hi sir, thank you so much for your help,can you help with this question:Hi sir,can you help with this question https://stackoverflow.com/questions/68090116/pandas-expected-10-fields-in-line-153-saw-11-how-to-add-one-more-column – William Jun 22 '21 at 20:05
  • Hi @Andrej friend,can you help me with this question https://stackoverflow.com/questions/68309137/how-to-cross-checking-2-pandas-dataframes-file-and-use-1-dataframes-value-as-a – William Jul 08 '21 at 22:09