how do i do conditional branching on a elements of a pandas series?

socnis

If I have a series S that has such mixed values

textelement
{"id":1,"name":"whatever","value":"sonso"}
name-value

how do I make a conditional statement so that when the element is in JSON format, it will skip, but when it's a text string or name-value pair, I will convert to JSON format?

jezrael

You can filter out json format by boolean indexing and mask by str.startswith:

s = pd.Series(['textelement',{"id":1,"name":"whatever","value":"sonso"}, 'name-value'])
print(s)
0                                        textelement
1    {'id': 1, 'value': 'sonso', 'name': 'whatever'}
2                                         name-value
dtype: object

#cast all values to string
s = s.astype(str)

#check which string starts with `{`
mask = s.str.startswith('{')

print (mask)
0    False
1     True
2    False
dtype: bool

print (~mask)
0     True
1    False
2     True
dtype: bool

#filter by inverted mask with ~    
s = s[~mask]

print (s)
0    textelement
2     name-value
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I do conditional compilation branching based on the real type used?

From Dev

How do you implement experiments with conditional branching in PsychoPy Builder?

From Dev

How do I multiply elements using conditional statement?

From Dev

How can I do a foreign key lookup of a series of elements?

From Dev

How do I add conditional counts as a new column in python pandas?

From Dev

How do I deal with Pandas Series data type that has NaN?

From Java

How do I convert a pandas Series or index to a Numpy array?

From Dev

How do I compare two Python Pandas Series of different lengths?

From Dev

how do I apply normalize function to pandas string series?

From Dev

How do I overload `__eq__` to compare pandas DataFrames and Series?

From Dev

how do I apply normalize function to pandas string series?

From Dev

How do I daily reset pandas time series cumsum?

From Dev

how do I card shuffle a pandas series quickly

From Dev

How do I do conditional tag rendering?

From Dev

How do I rewrite a series of conditional statements with Q promises in node.js?

From Java

How do I clean a list, and list of list of elements in a pandas dataframe?

From Dev

Where is the value when I do this in pandas Series

From Dev

How to do/workaround a conditional join in python Pandas?

From Dev

Python & Pandas: How to do conditional calculation

From Dev

How do I create a conditional SQL query

From Dev

How do I exit a script in a conditional statement?

From Dev

How do I get conditional output in a report?

From Dev

How do I represent 1 + "" in conditional formatting?

From Dev

How do I create a conditional SQL query

From Dev

How do I simplify a conditional with multiple or statements?

From Dev

How do I handle a complex set of related projects using TFS 2013 GIT repository and branching?

From Dev

How do I turn a dataframe into a series of lists?

From Dev

How do I turn a dataframe into a series of lists?

From Dev

How do i modify a teechart series value?

Related Related

  1. 1

    How can I do conditional compilation branching based on the real type used?

  2. 2

    How do you implement experiments with conditional branching in PsychoPy Builder?

  3. 3

    How do I multiply elements using conditional statement?

  4. 4

    How can I do a foreign key lookup of a series of elements?

  5. 5

    How do I add conditional counts as a new column in python pandas?

  6. 6

    How do I deal with Pandas Series data type that has NaN?

  7. 7

    How do I convert a pandas Series or index to a Numpy array?

  8. 8

    How do I compare two Python Pandas Series of different lengths?

  9. 9

    how do I apply normalize function to pandas string series?

  10. 10

    How do I overload `__eq__` to compare pandas DataFrames and Series?

  11. 11

    how do I apply normalize function to pandas string series?

  12. 12

    How do I daily reset pandas time series cumsum?

  13. 13

    how do I card shuffle a pandas series quickly

  14. 14

    How do I do conditional tag rendering?

  15. 15

    How do I rewrite a series of conditional statements with Q promises in node.js?

  16. 16

    How do I clean a list, and list of list of elements in a pandas dataframe?

  17. 17

    Where is the value when I do this in pandas Series

  18. 18

    How to do/workaround a conditional join in python Pandas?

  19. 19

    Python & Pandas: How to do conditional calculation

  20. 20

    How do I create a conditional SQL query

  21. 21

    How do I exit a script in a conditional statement?

  22. 22

    How do I get conditional output in a report?

  23. 23

    How do I represent 1 + "" in conditional formatting?

  24. 24

    How do I create a conditional SQL query

  25. 25

    How do I simplify a conditional with multiple or statements?

  26. 26

    How do I handle a complex set of related projects using TFS 2013 GIT repository and branching?

  27. 27

    How do I turn a dataframe into a series of lists?

  28. 28

    How do I turn a dataframe into a series of lists?

  29. 29

    How do i modify a teechart series value?

HotTag

Archive