我有一个带有4列的pandas数据框,我想制作第五列,这是一个包含4列元素的列表 例如:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
>>> df
A B C D
0 -0.095531 1.922726 0.333087 0.173920
1 -2.633423 -0.635973 1.059518 -1.129433
2 -1.579580 1.088753 0.462434 -0.349215
3 -0.129472 0.656815 0.191109 -0.631818
4 -1.977579 -0.559613 0.789966 -0.044518
5 0.706840 -2.005750 0.327085 1.106847
我希望附加一个列E,以便第一行看起来像
[A=-0.095531,B=1.922726,C=0.333087,D=0.173920]
我尝试df['E'] = list(df.values)
但是生成了op
[-0.095531,1.922726,0.333087,0.173920]
。
答案 0 :(得分:0)
我为所有列做了df ['A'] =“A =”+ df ['A']。astype(str)等等。然后df ['E'] = list(df.values)给出了所需的op
答案 1 :(得分:0)
我认为以下内容可能有所帮助:
df['E']=list(df.values)
df
A B C D E
0 0.879163 0.078845 -0.678123 1.985986 [0.879162590333, 0.0788449197234, -0.678123390...
1 2.583740 -0.604236 -1.530671 -0.338767 [2.58373987111, -0.604235978563, -1.5306710724...
2 -0.435389 -0.654454 -1.170191 -1.829236 [-0.435389198028, -0.65445422574, -1.170190542...
3 -0.009336 -0.582220 0.177863 -0.014115 [-0.00933610605622, -0.582219961202, 0.1778632...
4 -2.044836 0.519311 1.626044 -0.303060 [-2.0448364492, 0.51931139324, 1.62604416428, ...
5 -1.244811 0.253653 0.450925 -0.410422 [-1.24481148127, 0.253652735816, 0.45092490489...
答案 2 :(得分:0)
不确定这有用!但是你走了。
df.assign(
E=df.applymap(
'{: 5.2f}'.format
).radd(df.columns.to_series() + '=').values.tolist()
)
A B C D E
0 -0.095531 1.922726 0.333087 0.173920 [A=-0.10, B= 1.92, C= 0.33, D= 0.17]
1 -2.633423 -0.635973 1.059518 -1.129433 [A=-2.63, B=-0.64, C= 1.06, D=-1.13]
2 -1.579580 1.088753 0.462434 -0.349215 [A=-1.58, B= 1.09, C= 0.46, D=-0.35]
3 -0.129472 0.656815 0.191109 -0.631818 [A=-0.13, B= 0.66, C= 0.19, D=-0.63]
4 -1.977579 -0.559613 0.789966 -0.044518 [A=-1.98, B=-0.56, C= 0.79, D=-0.04]
5 0.706840 -2.005750 0.327085 1.106847 [A= 0.71, B=-2.01, C= 0.33, D= 1.11]
根据您的字面要求:
df.assign(
E=df.astype(str).radd(df.columns.to_series() + '=').values.tolist())
A B C D E
0 -0.095531 1.922726 0.333087 0.173920 [A=-0.095531, B=1.922726, C=0.333087, D=0.17392]
1 -2.633423 -0.635973 1.059518 -1.129433 [A=-2.633423, B=-0.635973, C=1.059518, D=-1.12...
2 -1.579580 1.088753 0.462434 -0.349215 [A=-1.57958, B=1.088753, C=0.462434, D=-0.349215]
3 -0.129472 0.656815 0.191109 -0.631818 [A=-0.129472, B=0.656815, C=0.191109, D=-0.631...
4 -1.977579 -0.559613 0.789966 -0.044518 [A=-1.977579, B=-0.559613, C=0.789966, D=-0.04...
5 0.706840 -2.005750 0.327085 1.106847 [A=0.70684, B=-2.00575, C=0.327085, D=1.106847]