Pandas 如何格式化/连接数据帧中的数据?
在R中,我要做的是这样的:Pandas 如何格式化/连接数据帧中的数据?,pandas,Pandas,在R中,我要做的是这样的: df$C <- sprintf("%02d-%02d", df$A, df$B) 在pandas中 df.apply(lambda x : "%02d-%02d" % (x['A'],x['B']),axis=1) Out[218]: 0 01-04 1 02-05 2 03-06 dtype: object 妈的,我知道这必须很简单。 df.apply(lambda x : "%02d-%02d" % (x['A'],x['B'])
df$C <- sprintf("%02d-%02d", df$A, df$B)
在
pandas中
df.apply(lambda x : "%02d-%02d" % (x['A'],x['B']),axis=1)
Out[218]:
0 01-04
1 02-05
2 03-06
dtype: object
妈的,我知道这必须很简单。
df.apply(lambda x : "%02d-%02d" % (x['A'],x['B']),axis=1)
Out[218]:
0 01-04
1 02-05
2 03-06
dtype: object
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({"A": [1,2,3], "B": [4,5,6]})
In [9]: df['C']= df.apply(lambda x:"{:02d}-{:02d}".format(x['A'],x['B']), axis=1)
In [10]: df
Out[12]:
A B C
0 1 4 01-04
1 2 5 02-05
2 3 6 03-06