1

Please, it would be great if someone gives me a clue as to resolve my willing to filter data with str 'Трафик с мобильных устройств'. Now, if I run this code:

data.query('Segment == Трафик с мобильных устройств')

I'll get this message: Segment ==Трафик с мобильных устройств ^ SyntaxError: invalid syntax

If do this:

data[data['Segment] == Трафик с мобильных устройств']

I will get: data[data['Segment] == Трафик с мобильных устройств']

I have: enter image description here

I want: enter image description here

1
  • Sorry, I have done a mistake. In the second case, I will get this message "SyntaxError: unexpected EOF while parsing" Commented Feb 1, 2020 at 7:45

1 Answer 1

1

Missing "", if first ':

data.query('Segment == "Трафик с мобильных устройств"')
#alternative with swapped " -> '
#data.query("Segment == 'Трафик с мобильных устройств'")

For second:

data[data['Segment'] == 'Трафик с мобильных устройств']
#alternative
#data[data["Segment"] == "Трафик с мобильных устройств"]

Sample:

data = pd.DataFrame({'Segment':['Трафик с мобильных устройств',
                                'Трафик',
                                'Трафик с мобильных устройств']})

df = data.query('Segment == "Трафик с мобильных устройств"')
print (df)
                        Segment
0  Трафик с мобильных устройств
2  Трафик с мобильных устройств

df = data[data['Segment'] == 'Трафик с мобильных устройств']
print (df)
                        Segment
0  Трафик с мобильных устройств
2  Трафик с мобильных устройств
Sign up to request clarification or add additional context in comments.

5 Comments

@ ezraelcool, but if I run this I got it UndefinedVariableError: name 'Segment' is not defined, the same the second KeyError: 'Segment'
@Andrew - It is not column name? what is print (data.columns.tolist()) ?
['Category', 'Month', 'Segment ', 'Revenue', 'Quantity ', 'Average_price', 'Average_quantity']
@Andrew - Added sample data, for me working perfectly.
@ ezrael Got it, I have a space in 'Segment ', it works ) Thank you!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.