0

I have a string column foo in my DataFrame. I need to create a new column bar, whose values are derived from corresponding foo values by a sequence of string-processing operations - a bunch of str.split()s and str.join()s in this particular case.

What is the most efficient way to do this?

2 Answers 2

1

Take a look at the vectorized string methods of pandas dataframes. http://pandas.pydata.org/pandas-docs/dev/text.html#text-string-methods

# You can call whatever vectorized string methods on the RHS
df['bar'] = df['foo']

eg.

df = pd.DataFrame(['a c', 'b d'], columns=['foo'])
df['bar'] = df['foo'].str.split(' ').str.join('-')
print(df)

yields

   foo  bar
0  a c  a-c
1  b d  b-d
Sign up to request clarification or add additional context in comments.

Comments

1

Pandas can do this for you. A simple example might look like:

foo = ["this", "is an", "example!"]

df = pd.DataFrame({'foo':foo})
df['upper_bar'] = df.foo.str.upper()
df['lower_bar'] = df.foo.str.lower()
df['split_bar'] = df.foo.str.split('_')
print(df)

which will give you

       foo   upper_bar  lower_bar   split_bar
0      this      THIS      this      [this]
1     is an     IS AN     is an     [is an]
2  example!  EXAMPLE!  example!  [example!]

See the link above from Alex

Comments

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.