I want to extract some characters from every cell in every column of a pandas dataframe. MWE below:
import pandas as pd
# initialize list of lists
data = [['tom', 'jerry'], ['nick', 'fury'], ['juli', 'anna']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Name', 'Name2'])
df2 = df['Name'].str[:1] # this works for a single column
df3 = df.map(str[:1]) # this does not work for the entire dataframe
`
I was able to remove the strings from a single column (df2) but couldn’t do it for the entire dataframe (df3 attempt). The error I am getting is TypeError: type 'str' is not subscriptable
New contributor
ktp89 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.