我有一列电话号码(作为字符串)。然后还有另一列给我零的数量(如int),我想附加到现有的电话号码。我想做的是像(5 *“0”=>“00000”)。还有一个先决条件。如果电话号码以“1”结尾,则只能添加零。
Example:
>>> df = pd.DataFrame([["11", 2], ["21", 3], ["10", 6]], columns=['phone', 'ext_length'])
What I tried:
>>> df.loc[(df.phone.str.endswith("1")), "complete_phone"] = df.phone + (df.ext_length * "0")
在过滤手机结束于“1”并且创建“complete_phone”列的正确行时,我无法使“数学”工作。我正在
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')
我也不理解错误信息,也不知道如何解决这个问题。
PS:我也在寻找一个链接,它显示了如何正确包含python示例,正如我在[in:]和[out:]加上结果的其他问题中看到的那样。任何提示?
答案 0 :(得分:1)
如果条件为True
,我认为您需要mask
替换
str.repeat
:
s = pd.Series(['0'], index=df.index)
mask = df.phone.str.endswith("1")
df["complete_phone"] = df.phone.mask(mask, df.phone + s.str.repeat(df.ext_length))
print (df)
phone ext_length complete_phone
0 11 2 1100
1 21 3 21000
2 10 6 10
DataFrame.apply
的另一个解决方案:
mask = df.phone.str.endswith("1")
df["complete_phone"] = df['phone'].mask(mask, df.apply(lambda x: x['phone'] +
'0' * x.ext_length, axis=1))
print (df)
phone ext_length complete_phone
0 11 2 1100
1 21 3 21000
2 10 6 10
mask = df.phone.str.endswith("1")
df["complete_phone"] = df.phone.mask(mask, df['phone'] +
df['ext_length'].apply(lambda x:'0'*x))
print (df)
phone ext_length complete_phone
0 11 2 1100
1 21 3 21000
2 10 6 10
如果掩码为NaN
,则您的解决方案类似于False
:
mask = df.phone.str.endswith("1")
df.loc[mask, "complete_phone"] = df['phone'] + df.apply(lambda x: '0' * x.ext_length, axis=1)
phone ext_length complete_phone
0 11 2 1100
1 21 3 21000
2 10 6 NaN
答案 1 :(得分:0)
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([["11", 2], ["21", 3], ["10", 6]], columns=['phone', 'ext_length'])
In [3]: df
Out[3]:
phone ext_length
0 11 2
1 21 3
2 10 6
In [4]: df['complete_phone'] = [number+'0'*length if number.endswith('1') else number for number, length in zip(df.phone, df.ext_length)]
In [5]: df
Out[5]:
phone ext_length complete_phone
0 11 2 1100
1 21 3 21000
2 10 6 10
答案 2 :(得分:0)
我认为ostream &operator<<(ostream &out, const BigInteger &t)
功能派上用场了:
apply