Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

python - Change the value of a cell having particular pattern in Pandas


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is one way to do it:

I made a simple test dataframe:

df = pd.DataFrame({'z': [1.525, -1.234, -2.345, '1.32-2', '-2.54-5']})

This tries to convert things to nueric values, but will put a "NaN" in the places it fails.

df['b'] = pd.to_numeric(df['z'], errors='coerce')

Then I make a column where everything is a string, and overwrite the entries that were "NaN" when trying to convert the first time. The overwriting uses a regular expression to make the failed scientific notation "-" into the string "e-""

df['c'] = df['z'].astype(str)
df.loc[df['b'].isna(), 'c'] = df['z'].str.replace(r'(d+)(-)(d+)', r'1e-3', regex=True)

Now that the string format is correct, this can convert them.

df['d'] = pd.to_numeric(df['c'])

Here is the dataframe:

print(df)
         a      b         c         d
0    1.525  1.525     1.525  1.525000
1   -1.234 -1.234    -1.234 -1.234000
2   -2.345 -2.345    -2.345 -2.345000
3   1.32-2    NaN   1.32e-2  0.013200
4  -2.54-5    NaN  -2.54e-5 -0.000025

Then just grab the column you want:

df_new = df[['d']].copy().rename(columns={'d':'z'})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...