Pandas 中数据集的复杂转换 [英] A complex transformation of a data set in pandas
本文介绍了Pandas 中数据集的复杂转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框:
dictionary = {'Year': [1985, 1985, 1986, 1986, 1987, 1987], 'Wteam' :[1, 2, 3, 4, 5, 6], 'lteam': [ 9, 10, 11, 12, 13, 14] }
pdf = pd.DataFrame(dictionary)
Wteam Year lteam
0 1 1985 9
1 2 1985 10
2 3 1986 11
3 4 1986 12
4 5 1987 13
5 6 1987 14
我需要按以下格式创建一个新的数据框:
I need to create a new data frame in the following format :
team values predicted_value
1985_1_9 1
1985_2_10 1
1986_3_11 1
1986_4_12 1
1987_5_13 1
1987_6_13 1
我的新数据框的值应采用year_Wteam_lteam"这种格式.我如何在熊猫中做到这一点.预测值列始终为 1.
The values of my new data frame should be in this format "year_Wteam_lteam". How do I do that in pandas. The predicted values column is always 1.
推荐答案
您可以执行以下操作:
final = pd.DataFrame()
final['team values'] = pdf['Year'].astype('str') + '_' + pdf['Wteam'].astype('str') + '_' + pdf['lteam'].astype('str')
final['predicted_value'] = 1
这篇关于Pandas 中数据集的复杂转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文