简体   繁体   English

根据条件更改具有混合类型的 dataframe 列中的值

[英]Change values in a dataframe column with mixed types based on a condition

One column of my dataset has both strings and floats.我的数据集的一列既有字符串又有浮点数。 In that column, for each string I am trying to replace it with only the first 5 characters of the string.在该列中,对于每个字符串,我仅尝试将其替换为字符串的前 5 个字符。

def isfloat(num):
    try:
        float(num)
        return True
    except ValueError:
        return False

df = pd.DataFrame([[1, "Alligator"], [1, 3], [4, "Markets"]], columns=['A', 'B'])

The following two methods don't seem to change the actual dataframe.以下两种方法似乎并没有改变实际的dataframe。

df['B'].apply(lambda x: float(x) if isfloat(x) else x[0:5])

for index, row in df.iterrows():
    if not isfloat(row.B):
        row.B = row.B[0:5]

This next method results in the warning "cannot convert the series to <class 'float'>", I think because the isfloat method cannot be called in this way.下一个方法导致警告“无法将系列转换为 <class 'float'>”,我认为是因为无法以这种方式调用 isfloat 方法。

df['B'] = np.where(not isfloat(df['B']), df['B'][0:5], df['B'])

I tried using.loc as well but it did not seem suitable because of the condition I need to base the change on.我也尝试过 using.loc ,但它似乎不合适,因为我需要根据条件进行更改。 How would one go about this, or what am I missing?一个 go 怎么会这样,或者我错过了什么?

I believe you need:我相信你需要:

df['B']=df['B'].apply(lambda x: float(x) if isfloat(x) else x[0:5])

Since DataFrames are not edited in place.由于 DataFrame 没有就地编辑。

Output: Output:

   A      B
0  1  Allig
1  1    3.0
2  4  Marke

Hi first of all dataframes are not edited in place.嗨,首先数据框没有就地编辑。 you simply need to store edited value of df.B column again in df.B column.您只需将 df.B 列的编辑值再次存储在 df.B 列中。

df.B=df.B.apply(lambda x: float(x) if isfloat(x) else x[0:5])

Also You can use the below Code too:您也可以使用以下代码:

import pandas as pd
df = pd.DataFrame([[1, "Alligator"], [1, 3], [4, "Markets"]], columns=['A', 'B'])
newlist=[]   
for v in df.B:
    if type(v)==str:
        newlist.append(v[:5])
    else:
        newlist.append(v)
df['B']=newlist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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