0

I have a text file called data.txt that looks like:

n   sin(n)  cos(n)
0   0   1
1   0.841470985 0.540302306
2   0.909297427 -0.416146837
3   0.141120008 -0.989992497
4   -0.756802495    -0.653643621
5   -0.958924275    0.283662185
6   -0.279415498    0.960170287
7   0.656986599 0.753902254
8   0.989358247 -0.145500034
9   0.412118485 -0.911130262
10  -0.544021111    -0.839071529

I am trying to extracting these columns of data into a pandas dataframe.

What I am doing right now is:

col1 = []
col2 = []
col3 = []

with open('data.txt', 'r') as f:
    for line in f:
        first, second, third = line.split()
        col1.append(first)
        col2.append(second)
        col3.append(third)

print(col1)
print(col2)
print(col3)

This code reads data.txt line by line and it gets pretty slow if I have huge data files.

Is there a way to streamline this using something like pandas? Is it possible to extract these columns using pandas?

1

1 Answer 1

2

I think this should help you:

data = pd.read_csv('file_name.txt',sep= " ")

This will give you dataframe and its very easy to compute such problem using pandas dataframe.

Good Luck

Sign up to request clarification or add additional context in comments.

1 Comment

this works! just that i had to change it to sep='\t'

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.