且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Pandas 数据框拆分为多列

更新时间:2022-12-12 10:11:04

我猜您正在尝试为 DF 调用ROW"属性,但该属性不存在.如果您正在尝试执行选择行的操作,我建议 .itterows() 调用仅在选择索引处循环您想要的行!这是我认为您要实现的***解决方案:)

My guess is you are attempting to call the "ROW" attribute for the DF, which does not exist. If you are attempting to do operations to select rows, I would suggest the .itterows() call to loop over just the rows you want at select indexes! Here is the best solution for what I think you are trying to achieve :)

import pandas as pd

重新创建的虚拟数据

content = ['cl_bob_lower_amt "0"',
'cl_bobamt_lat "0"',
'cl_bobamt_vert "0"',
'cl_bobcycle "2"',
'cl_viewmodel_shift_left_amt "0"',
'cl_viewmodel_shift_right_amt "0"',]

原始数据框创建

df = pd.DataFrame(content, columns = ['Value'])

通过对 Value.str 使用 .split 调用创建一个新的数据帧(或重新分配给现有的 DF)

Create a new dataframe (or re-assign to existing DF) by using .split call on Value.str

split_df = pd.DataFrame(df.Value.str.split(" ").tolist(), columns=["Command", "Value"])

结果:

                        Command   Value
0              cl_bob_lower_amt   "0"
1                 cl_bobamt_lat   "0"
2                cl_bobamt_vert   "0"
3                   cl_bobcycle   "2"
4   cl_viewmodel_shift_left_amt   "0"
5  cl_viewmodel_shift_right_amt   "0"