数据处理知识总结

tech2022-08-08  132

1.如何对python中的list中所有元素进行类型转换?

# helper function to type cast list 使用map操作

def cast_list(test_list, data_type):

    return list(map(data_type, test_list))

      

# helper function to type cast Matrix 使用lambda函数

def cast_matrix(test_matrix, data_type):

    return list(map(lambda sub: list(map(data_type, sub)), test_matrix))

 

注意进行map操作以后注意转换为list

样例代码:

import numpy as np from mayavi import mlab import pandas as pd df=pd.read_csv('~/vicon/nexusdata.csv',header=3) #ball1 col_X1 = df['X1'][1:2000].tolist() col_Y1 = df['Y1'][1:2000].tolist() col_Z1 = df['Z1'][1:2000].tolist() pos_X1 = list(map(float,col_X1)) pos_Y1 = list(map(float,col_Y1)) pos_Z1 = list(map(float,col_Z1))

 

 

2.pandas读取csv文件如何忽略文件头一行或者头几行?

设置header参数

headerint, list of int, default ‘infer’

Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.

 

headerint,整数列表,默认为“推断” 行号(用作列名)以及数据的开头。 默认行为是推断列名:如果未传递名称,则行为与header = 0相同,并且从文件的第一行推断出列名;如果显式传递列名,则该行为与header = None相同。 。 显式传递header = 0以便能够替换现有名称。 标头可以是整数列表,这些整数指定列上的多索引的行位置,例如 [0,1,3]。 未指定的中间行将被跳过(例如,在此示例中为2)。 请注意,如果skip_blank_lines = True,则此参数将忽略注释行和空行,因此header = 0表示数据的第一行,而不是文件的第一行。

 

 

样例代码:

数据格式: Trajectories,,,,,,,,,,,,, 100,,,,,,,,,,,,, ,,ball01:11,,,ball01:21,,,ball01:31,,,ball01:41,, Frame,Sub Frame,X1,Y1,Z1,X2,Y2,Z2,X3,Y3,Z3,X4,Y4,Z4 ,,mm,mm,mm,mm,mm,mm,mm,mm,mm,mm,mm,mm 1,0,102.767,322.755,607.975,20.5028,493.519,723.674,-114.053,499.876,600.554,-200.936,357.938,788.527 2,0,102.413,323.126,607.977,20.5319,493.501,723.679,-114.011,499.607,600.541,-201.059,357.861,788.557 3,0,102.023,323.41,607.963,20.5597,493.468,723.679,-113.986,499.384,600.531,-201.151,357.779,788.563 4,0,101.721,323.788,607.984,20.6039,493.468,723.699,-113.956,499.136,600.533,-201.241,357.689,788.575 5,0,101.329,324.182,608.004,20.6536,493.491,723.758,-113.937,498.979,600.53,-201.347,357.479,788.572 6,0,100.996,324.487,608.004,20.706,493.524,723.798,-113.926,498.826,600.567,-201.482,357.156,788.543 7,0,100.643,324.901,607.998,20.774,493.549,723.838,-113.918,498.627,600.591,-201.583,356.842,788.546 处理代码: import numpy as np from mayavi import mlab import pandas as pd df=pd.read_csv('~/vicon/nexusdata.csv',header=3) df前几行数据展示: df = pd.read_csv('~/vicon/nexusdata.csv', header=3) >>> df Frame Sub Frame X1 Y1 Z1 ... Y3 Z3 X4 Y4 Z4 0 NaN NaN mm mm mm ... mm mm mm mm mm 1 1.0 0.0 102.767 322.755 607.975 ... 499.876 600.554 -200.936 357.938 788.527 2 2.0 0.0 102.413 323.126 607.977 ... 499.607 600.541 -201.059 357.861 788.557 3 3.0 0.0 102.023 323.41 607.963 ... 499.384 600.531 -201.151 357.779 788.563 4 4.0 0.0 101.721 323.788 607.984 ... 499.136 600.533 -201.241 357.689 788.575

 

 
最新回复(0)