具有如下数据-我需要将地址和Trans分为2列-可以帮助在python中使用melt或任何其他函数吗?
Id name address1 tran1 address2 tran2 address32 tran3 1 Vasu chennai 3432 tamb 4432 cam 0121 2 het tamil 32432 cheai 43543 vsda 3432 3 sfds andh 12321 hyd 6754 nizh 7657
需要此数据为
************************** Id******name*****address*****Tran 1*****Vasu******chennai*****3432 2****het********tamil*******32432 3***sfds******* andh*******12321 1***Vasu*******tamb********4432 2*****het******cheai*******43543 3*****sfds****hyd**********6754 1****Vasu*****cam*********0121 2****het*****vsda********3432 3****sfds****nizh*******7657
谢谢
尝试通过这种方式以不同的方式获得结果
data = pd.read_csv('C:DOCUMENTS/SAMP.csv') melt_data1 = pd.melt(data, id_vars=['id', 'name'], value_vars=['address1','tran1','address2','tran2','address32','tran3'], var_name='Address', 'tRAN', value_name='Values') melt_data1.to_csv('c:/DOCUMENTS/SAM.CSV)
答案 0 :(得分:0)
您只需要为此使用pd.concat:
data1 = data.loc[:,['name','address1','tran1']].rename(columns={"address1":
"address", "tran1": "tran"})
data2 = data.loc[:,['name','address2','tran2']].rename(columns={"address2":
"address", "tran2": "tran"})
data3 = data.loc[:,['name','address3','tran3']].rename(columns={"address3":
"address", "tran3": "tran"})
pd.concat([ data1 , data2 , data3])