Answer a question

My data file looks like this:

data.txt
user,activity,timestamp,x-axis,y-axis,z-axis
0,33,Jogging,49105962326000,-0.6946376999999999,12.680544,0.50395286;
1,33,Jogging,49106062271000,5.012288,11.264028,0.95342433;
2,33,Jogging,49106112167000,4.903325,10.882658000000001,-0.08172209;
3,33,Jogging,49106222305000,-0.61291564,18.496431,3.0237172;

As can be seen, the last column ends with a semicolon, so when I read into pandas, the column is inferred as type object (ending with the semicolon.

df = pd.read_csv('data.txt')
df
    user    activity    timestamp   x-axis  y-axis  z-axis
0   33  Jogging     49105962326000  -0.694638   12.680544   0.50395286;
1   33  Jogging     49106062271000  5.012288    11.264028   0.95342433;
2   33  Jogging     49106112167000  4.903325    10.882658   -0.08172209;
3   33  Jogging     49106222305000  -0.612916   18.496431   3.0237172;

How do I make pandas ignore that semicolon?

Answers

The problem with your txt is that it has mixed content. As I can see the header doesn't have the semicolon as termination character

If you change the first line adding the semicolon it's quite simple

pd.read_csv("data.txt", lineterminator=";")
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐