使用 pd.read_csv 创建数据框,但列中的数据已连接
Create data frame with pd.read_csv but data in column is connected
以下数据是大数据集的一小部分。
-.976201 -.737468 -.338866 -.174108 -.388671 -.793479 -1.063547 -1.005576
-.666256 -.254177 .018064 .069349 -.015640 -.090710 -.111850 -.194042
-.486229 -.993744 -1.554215 -2.003795 -2.348716 -2.770146 -3.502312 -4.712848
-6.401421 -8.300894 -9.896770-10.674380-10.444660 -9.438081 -8.065303 -6.594510
我本质上想做的是将数据转换为数据框并附加一个时间列,但是,我 运行 在集合的最后一行遇到了麻烦,因为点 a 由连字符。
数据集中的几行就是这种情况,但我不知道如何解决这个问题。最终,我想绘制数据,因此需要删除 Motion 列的 dtype: 对象。
它给我的数据框显示在附加图片中,这是我的代码:
Dataframe print
import numpy as np
import pandas as pd
time_range = np.arange(0, 500, 0.005)
motion_data = pd.read_csv('data.txt', header = None, sep = "\s+", names = range(0, 8, 1))
motion_frame = pd.DataFrame(motion_data)
motion_frame = motion_frame.stack(dropna=False).reset_index(drop=True).to_frame('Motion')
time = pd.DataFrame(time_range, index = None)
motion_frame['Time'] = time
motion_frame['Motion'].str.split('-', expand=True)
# motion_frame['Motion'].astype('float')
print(motion_frame)
motion_frame.dtypes
查看您的数据,每列都是 10 个字符宽。如果为真,则可以使用 pandas.read_fwf()
方法并指定 'widths='
.
例如:
import numpy as np
import pandas as pd
time_range = np.arange(0, 500, 0.005)
motion_data = pd.read_fwf('data.txt', widths=[10] * 8, names = range(0, 8, 1))
motion_frame = pd.DataFrame(motion_data)
motion_frame = motion_frame.stack(dropna=False).reset_index(drop=True).to_frame('Motion')
time = pd.DataFrame(time_range, index = None)
motion_frame['Time'] = time
motion_frame['Motion'] = motion_frame['Motion'].astype('float')
print(motion_frame)
print(motion_frame.dtypes)
打印:
Motion Time
0 -0.976201 0.000
1 -0.737468 0.005
...
30 -8.065303 0.150
31 -6.594510 0.155
Motion float64
Time float64
dtype: object
以下数据是大数据集的一小部分。
-.976201 -.737468 -.338866 -.174108 -.388671 -.793479 -1.063547 -1.005576
-.666256 -.254177 .018064 .069349 -.015640 -.090710 -.111850 -.194042
-.486229 -.993744 -1.554215 -2.003795 -2.348716 -2.770146 -3.502312 -4.712848
-6.401421 -8.300894 -9.896770-10.674380-10.444660 -9.438081 -8.065303 -6.594510
我本质上想做的是将数据转换为数据框并附加一个时间列,但是,我 运行 在集合的最后一行遇到了麻烦,因为点 a 由连字符。 数据集中的几行就是这种情况,但我不知道如何解决这个问题。最终,我想绘制数据,因此需要删除 Motion 列的 dtype: 对象。 它给我的数据框显示在附加图片中,这是我的代码: Dataframe print
import numpy as np
import pandas as pd
time_range = np.arange(0, 500, 0.005)
motion_data = pd.read_csv('data.txt', header = None, sep = "\s+", names = range(0, 8, 1))
motion_frame = pd.DataFrame(motion_data)
motion_frame = motion_frame.stack(dropna=False).reset_index(drop=True).to_frame('Motion')
time = pd.DataFrame(time_range, index = None)
motion_frame['Time'] = time
motion_frame['Motion'].str.split('-', expand=True)
# motion_frame['Motion'].astype('float')
print(motion_frame)
motion_frame.dtypes
查看您的数据,每列都是 10 个字符宽。如果为真,则可以使用 pandas.read_fwf()
方法并指定 'widths='
.
例如:
import numpy as np
import pandas as pd
time_range = np.arange(0, 500, 0.005)
motion_data = pd.read_fwf('data.txt', widths=[10] * 8, names = range(0, 8, 1))
motion_frame = pd.DataFrame(motion_data)
motion_frame = motion_frame.stack(dropna=False).reset_index(drop=True).to_frame('Motion')
time = pd.DataFrame(time_range, index = None)
motion_frame['Time'] = time
motion_frame['Motion'] = motion_frame['Motion'].astype('float')
print(motion_frame)
print(motion_frame.dtypes)
打印:
Motion Time
0 -0.976201 0.000
1 -0.737468 0.005
...
30 -8.065303 0.150
31 -6.594510 0.155
Motion float64
Time float64
dtype: object