I need to select multiple strings that ends with ['edit'] make new column name 'state' from this dataframe.
Asked
Active
Viewed 67 times
-2
-
1Please provide a [reproducible pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – BioGeek Feb 24 '20 at 00:24
-
What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [tour], [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Feb 24 '20 at 01:18
2 Answers
0
This should work:
df['state'] = df.loc[df['YourCol'].str.endswith('[edit]'), 'YourCol']
Just replace YourCol
with the column you're working with, and df
with the name of your dataframe
. Note that where it doesn't end with [edit]
, you will get NaN
values.

Nicolas Gervais
- 33,817
- 13
- 115
- 143
0
I think you're trying to use the "[edit]" rows not just as a separate column, instead map them to the schools below them, so that you'd end up with something like this:
School State
Fairbanks (University of Alaska Fairbanks)[2] Alaska
Flagstaff (Northern Arizona University) Arizona
Tempe (Arizona State University) Arizona
Tucson (University of Arizona) Arizona
Arkadelphia (Henderson State University, ...) Arkansas
If that's the case, you can use a combination of Nicolas Gervais's answer with pandas' fillna
:
df['State'] = df[df['YourCol'].str.endswith('[edit]')]['YourCol']
df['State'] = df['State'].fillna(method='ffill') # Forward-fills the states
df = df.drop(df[df['YourCol'].str.endswith('[edit]')]) # Drop the "header" rows
df['State'] = df['State'].str.slice_replace(start=-6) # Remove "[edit]" from the end of the state names

Adam Kern
- 566
- 4
- 14