检查文本在 python 中是否右对齐

check text is right align or not in python

我有这样一个数据框:

  |number  |
1 |122     |
2 |345     | 
3 |     456|
4 |     789|

我想改成:

  |number|
1 |nan|
2 |nan| 
3 |456|
4 |789|

我只想右对齐值

您可以使用 series.str.split() to split the strings and grab the end element from the split and convert to numeric using pd.to_numeric:

df['number']=pd.to_numeric(df['number'].str.split(' ').str[-1],errors='coerce')

测试于:

s=pd.Series(['122 ','345 ',' 456',' 789'])
pd.to_numeric(s.str.split(' ').str[-1],errors='coerce')

0      NaN
1      NaN
2    456.0
3    789.0
dtype: float64

我认为您需要测试 Series.str.endswith if space, then replace to NaN by Series.mask, remove possible traling spaces by Series.str.strip 并转换为浮点数:

df['number'] = df['number'].mask(df['number'].str.endswith(' ')).str.strip().astype(float)

或:

df['number'] = df['number'].mask(df['number'].str[-1] == ' ').str.strip().astype(float)

print (df)
   number
1     NaN
2     NaN
3   456.0
4   789.0

如果想要整数,可以通过 integer na 在 pandas 0.24+:

中工作
m = df['number'].str.endswith(' ')
df['number'] = df['number'].mask(m).str.strip().astype(float).astype('Int64')

print (df)
   number
1     NaN
2     NaN
3     456
4     789