简体   繁体   中英

Is there a way to use values in a pandas dataframe column to perform a slice of on a string?

I would like to populate a column with slices taken from a string.
ie

df['unique'slice']= filler_slice[0:df[some_value']]

I have a dataframe column that contains strings ranging in length from 20:300 characters. I would like to make all of these strings the same length (300 char). The extra characters would come from a defined string. I have been trying to set the 300char column using the following but it does not work. I get an error length of values does not match length of ' ' index.

my code looks like this:

filler_string='IAMA300CHARFILLERSTRING'.
df['300_string']=df['smaller_string']+filler_string[0:300-df['smaller_string_len']]

The following snippet can be used to achieve the stated objective. Just a simulation of your problem. Some parts of the code can be modified to suit your code. The filler string is repeated for the max_len to handle the case where the difference between the required length and actual length is greater than the filler string length (if the filler string is of max length, then this can be omitted).

temp = {'string' : ['text1', 'text10', 'text123']}
df = pd.DataFrame(temp)
max_len = 300
filler_string='IAMA300CHARFILLERSTRING'
filler_string = (filler_string * (max_len // len(filler_string) + 1))[:max_len]
df['string'] = df['string'].apply(lambda x: "{}{}".format(x, filler_string[0:max_len - len(x)]))

Have you tried pandas's pad ?

df['smaller_string'].str.pad(width=300, side='right', fillchar=your_char)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM