Home > Enterprise >  How can I split this dataframe in multiple columns?
How can I split this dataframe in multiple columns?

Time:11-15

I'm working with a .txt archive, when I use pd.read_csv the rows look like that:

|0 | | -------- | | 2452791.268864 1.000268 0.000433 1 | . . .

I want this this numbers in 4 different columns, something like that:

| Column A | Column B | | -------- | -------- | | 2452791.268864 | 1.000268 | ... . . .

Note that the number of spaces between each number is different.

I'm so glad for your help!!!

I'm tried to use split functions but I don't get what I want.

CodePudding user response:

You can either set the delim_whitespace parameter of pandas.read_csv to True :

df = pd.read_csv("your_text_file.txt", header=None, delim_whitespace=True)

Or use "\s " (one space or more) as a separator :

df = pd.read_csv("your_text_file.txt", header=None, sep="\s ")

# Output :

print(df)

              0         1         2  3
0  2.452791e 06  1.000268  0.000433  1

print(len(df.columns))

4
  • Related