有一个data frame结构,其中一列是data frame组成的,其他的列都是str或者数值之类的,如何将一行中str一类的塞入df里?
我试过apply,结果很诡异。。。
函数里print出来的结果是想要的,但是返回以后拿到的结果city就都是 tyu一个数值了
高手求解。。。
import pandas as pd
def testcc(df1):
#tmpdf = df1
tmpdf = df1['c']
value = df1['a']
# value = 111
tmpdf['city']=value
print(tmpdf)
return tmpdf
df1 = pd.DataFrame({'a': ['qwe'],
'b': ['asd']})
df4 =df1
df3 = pd.DataFrame({'a': ['qwe']})
df2 = pd.DataFrame({'a': ['qwe', 'wer', 'ert', 'rty', 'tyu'],
'b': ['asd', 'sdf', 'dfg', 'fgh', 'ghj'],
'c': [df1,df3,df4,df1,df1]})
df2['d']=df2.apply(testcc,axis=1)
print('------------------')
print(df2['d'])
结果是
a b city
0 qwe asd qwe
a b city
0 qwe asd qwe
a city
0 qwe wer
a b city
0 qwe asd ert
a b city
0 qwe asd rty
a b city
0 qwe asd tyu
------------------
0 a b city
0 qwe asd tyu
1 a city
0 qwe wer
2 a b city
0 qwe asd tyu
3 a b city
0 qwe asd tyu
4 a b city
0 qwe asd tyu
Name: d, dtype: object