简体   繁体   中英

Is there a way to add a string character to the values in a column in periods?

I have a dataframe called "time_df" which has one column "tstamps" which is consists of string values.I need to add a "0" to row 0,5,10,.... till the end of the dataframe.

snapshot of time_df

Is there a function/method to accomplish this? Thanks.

Use Series.str.ljust :

Consider below df :

In [1883]: df = pd.DataFrame({'tstamps':['1606.9104', '1606.91046', '1606.9107']})

In [1884]: df
Out[1884]: 
      tstamps
0   1606.9104
1  1606.91046
2   1606.9107

Now, you have to make all rows length to 10 by adding 0 to the right wherever missing.

In [1889]: df['tstamps'] = df['tstamps'].str.ljust(10, '0')

In [1890]: df
Out[1890]: 
      tstamps
0  1606.91040
1  1606.91046
2  1606.91070

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