用Python中的[key:value]组合将多个列合并到一个列列表中
让我在开始这个问题之前指出,合并列不是一本词典。结果数据框在“组合”列中有方括号,因此它看起来像是格式为[key1:value1,key2:value2,等等]的数据框中的列表 我正在尝试从以下内容转换我的数据帧:用Python中的[key:value]组合将多个列合并到一个列列表中,python,pandas,dataframe,Python,Pandas,Dataframe,让我在开始这个问题之前指出,合并列不是一本词典。结果数据框在“组合”列中有方括号,因此它看起来像是格式为[key1:value1,key2:value2,等等]的数据框中的列表 我正在尝试从以下内容转换我的数据帧: import pandas as pd test = pd.DataFrame({'apples':['red','green','yellow'], 'quantity': [1,2,3],'tasteFactor':['yum','yum','yuck']}) apple
import pandas as pd
test = pd.DataFrame({'apples':['red','green','yellow'], 'quantity':
[1,2,3],'tasteFactor':['yum','yum','yuck']})
apples quantity tasteFactor
0 red 1 yum
1 green 2 yum
2 yellow 3 yuck
此格式将每行中的键与值组合到一个新列中:
apples quantity tasteFactor combined
0 red 1 yum ['apples':'red','quantity':'1','tastefactor':'yum']
1 green 2 yum ['apples':'green','quantity':'2','tastefactor':'yum']
2 yellow 3 yuck ['apples':'yellow','quantity':'3','tastefactor':'yuck']
试图将数据帧转换为每行的字典,但在将其转换为列表时遇到了困难
test['combined'] = test.to_dict(orient='records')
生成的新列不需要是实际的列表类型。它可能是一根绳子
以前在这里问过这个问题,但想澄清这个问题标题中的问题。
找到了以下密切相关的问题,并尝试了它们的派生,这让我半途而废,但似乎无法得到正确的格式
import pandas as pd
df = pd.DataFrame({'apples':['red','green','yellow'], 'quantity':
[1,2,3],'tasteFactor':['yum','yum','yuck']})
col_names = df.columns
def func(row):
global col_names
list_ = [str(b)+':'+str(a) for a,b in zip(row,col_names.values.tolist())]
return list_
x = list(map(func, df.values.tolist()))
df.loc[:,'combined'] = pd.Series(x)
# df
# apples quantity tasteFactor combined
# 0 red 1 yum [apples:red, quantity:1, tasteFactor:yum]
# 1 green 2 yum [apples:green, quantity:2, tasteFactor:yum]
# 2 yellow 3 yuck [apples:yellow, quantity:3, tasteFactor:yuck]
正如您提到的,生成的新列不需要是实际的列表类型
编辑:
di=test.T.to_dict()
test['Mapper']=test.index
test.Mapper.map(di)
test=test.assign(combined=test.Mapper.map(di).astype(str)).drop('Mapper',1)
test=test.combined.str.replace('{','[').str.replace('}',']')
test.combined[0]
Out[511]: "['apples': 'red', 'quantity': 1, 'tasteFactor': 'yum']"
是的,但我正在寻找方括号格式,它可能是字符串类型。@sweetnlow给我一个sec@sweetnlow已编辑,仅使用
str.replace
我运行了此代码。。。并得到了苹果数量口味因子组合0红色1百胜(a,p,p,l,e,s)1绿色2百胜(q,u,a,n,t,i,t,y)2黄色3恶心(t,a,s,t,e,F,a,c,t,o,r)编辑。请检查一下,谢谢!添加了单引号以使其在zip中列出a、b(行、列名称.值.tolist())]
di=test.T.to_dict()
test['Mapper']=test.index
test.Mapper.map(di)
test=test.assign(combined=test.Mapper.map(di).astype(str)).drop('Mapper',1)
test=test.combined.str.replace('{','[').str.replace('}',']')
test.combined[0]
Out[511]: "['apples': 'red', 'quantity': 1, 'tasteFactor': 'yum']"