0

Target

I have a pandas dataframe as shown below and would like to split it where there is a blank-space, separating the "command" and the float value.


Dataframe - df e.g:

                                        0
...
432                   cl_bob_lower_amt "0"
433                      cl_bobamt_lat "0"
434                     cl_bobamt_vert "0"
435                        cl_bobcycle "2"
436        cl_viewmodel_shift_left_amt "0"
437       cl_viewmodel_shift_right_amt "0"
...

My Attempt

I have attempted to use the solution described here

import pandas as pd
file_ = open("E:\\Location\\Tree\\123456\\730\\local\\cfg\\myconfig.cfg", 'r')
content = file_.read()
paths = content.split("\n")
df = pd.DataFrame(paths)
df = pd.DataFrame(df.row.str.split(' ',1).tolist(),columns = ['command','value'])
print df

However, I seem to be receiving the following error:

AttributeError: 'DataFrame' object has no attribute 'row'

Unfortunately I'm unable to identify why this occurs.


Expected Outcome:

                  command              value
...
432                   cl_bob_lower_amt "0"
433                      cl_bobamt_lat "0"
434                     cl_bobamt_vert "0"
435                        cl_bobcycle "2"
436        cl_viewmodel_shift_left_amt "0"
437       cl_viewmodel_shift_right_amt "0"
...

Ultimately I would like the user to be able to search for the command and change the value.

5
  • 2
    Make sure it is dataframe or series Commented Jun 25, 2018 at 19:59
  • @Wen Is it not a dataframe already? Commented Jun 25, 2018 at 20:04
  • @LearningToPython it could be a dataframe or a series. From what you've posted, we can't tell which one.It looks like a series because we don't see a column header. But you say it's a dataframe. Those are inconsistent and Wen is asking you to clarify. Commented Jun 25, 2018 at 20:08
  • @piRSquared Oh, Okay. Thanks for the clarification. At the moment the column header is simply 0. (Edited) Commented Jun 25, 2018 at 20:14
  • 1
    Try df = pd.DataFrame(df[0].str.split(' ',1).tolist(),columns = ['command','value']) (I just replaced the reference to df.row (unknown column) by df[0] (the correct reference) Commented Jun 25, 2018 at 20:15

2 Answers 2

1

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

Recreated Dummy Data

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"',]

Original Dataframe created

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

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"])

Results:

                        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"
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciated! Thanks.
0
df['command'], df['value'] = df["0"].str.split().str

df

                                    0                       command value
432              cl_bob_lower_amt "0"              cl_bob_lower_amt   "0"
433                 cl_bobamt_lat "0"                 cl_bobamt_lat   "0"
434                cl_bobamt_vert "0"                cl_bobamt_vert   "0"
435                   cl_bobcycle "2"                   cl_bobcycle   "2"
436   cl_viewmodel_shift_left_amt "0"   cl_viewmodel_shift_left_amt   "0"
437  cl_viewmodel_shift_right_amt "0"  cl_viewmodel_shift_right_amt   "0"

If the column is an integer 0

df['command'], df['value'] = df[0].str.split().str

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.