Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas-transform()在值为字符串时引发ValueError_Python_Pandas_Transform - Fatal编程技术网

Python Pandas-transform()在值为字符串时引发ValueError

Python Pandas-transform()在值为字符串时引发ValueError,python,pandas,transform,Python,Pandas,Transform,我有两个数据帧,最初我将其分割并成功地转换为字符串。我的目标是将值替换为“1:0”,并将“:1”添加到其余值中,但我得到一个错误,如图所示: >>>df1 ID quantity 0 007 48.0 1 001 37.0 2 006 37.0 3 111 37.0 4 001 34.0 5 101 37.0 >>>df2 ID quantity 0 001 16.0 1

我有两个数据帧,最初我将其分割并成功地转换为字符串。我的目标是将值替换为“1:0”,并将“:1”添加到其余值中,但我得到一个错误,如图所示:

>>>df1
   ID  quantity
0 007      48.0
1 001      37.0
2 006      37.0
3 111      37.0
4 001      34.0
5 101      37.0 

>>>df2
   ID  quantity
0 001      16.0
1 006      16.0
2 111      16.0
3 001      14.0
4 101      16.0
5 111      16.0

df1_to_df2=df1('ID').floordiv(df2('ID')).astype('Int64').astype(str)
>>>df1_to_df2
     quantity
 ID              
007      <NA>  #this must be replaced by "1:0"
001         2  #":1" should be added when only a digit is there
006         2
111         2
001         2
101         2

#Then I tried this:
df1_to_df2=df1_to_df2.transform(lambda x: replace(x,'1:0') if x=='<NA>' else x+':1')

#and it raises this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

非常感谢

根据您的描述,您似乎只需要转换列数量: 你能试一下下面的小改动吗?引用该列并添加x.replace而不是replace

df1_to_df2['quantity'].transform(lambda x: x.replace(x, '1:0') if x=='<NA>' else x+':1')
df1_到_df2['quantity'].转换(如果x=''否则x+':1',则lambda x:x.replace(x,'1:0'))
您还可以按自己的方式使用“变换”:

df = (df1.set_index('ID')//df2.set_index('ID')).fillna(0).astype(int).astype(str)
df.quantity = df.quantity.transform(lambda x: '1:0' if x=='0' else x+':1')

请分享预期的输出Hanks,我在提示后也添加了该部分。不,我无论如何都会得到ValueError。使用
np.where()
的第一种方法是一种很好的解决方法,但我希望
转换也能起作用。相反,它引发了相同的
ValueError
,我很难理解为什么即使去掉了NaNs也会抛出这个错误。我的转换工作正常。我已经测试过了。您确定没有忘记指定列吗?例如:
df['quantity']=df['quantity'].transform(lambda x:'1:0'如果x=='else x+':1')
yes,我确信。。。我也检查了地板分割后产生的所有值,我没有类似于
inf
的东西,所以我无法发现这个错误是如何产生的。
import pandas as pd
import io
import numpy as np

df1,df2 = map(lambda x: pd.read_csv(io.StringIO(x), sep='\s+', converters={'ID':str}), (s1,s2))

df = (df1.set_index('ID')//df2.set_index('ID')).fillna(0).astype(int)
df.quantity = np.where(df.quantity==0, '1:0', df.quantity.astype(str) + ':1')
df = df.reset_index().drop_duplicates()

print(df)
    ID quantity
0  001      2:1
4  006      2:1
5  007      1:0
6  101      2:1
7  111      2:1
df = (df1.set_index('ID')//df2.set_index('ID')).fillna(0).astype(int).astype(str)
df.quantity = df.quantity.transform(lambda x: '1:0' if x=='0' else x+':1')