[英]Add new 'x' number of columns in data frame using max value in specific column using python pandas
I have a pandas DataFrame, as below:我有一个熊猫数据框,如下所示:
From such DataFrame I want to generate the following output:从这样的 DataFrame 我想生成以下输出:
To obtain the required DataFrame I used below code.为了获得所需的数据帧,我使用了下面的代码。
first_list_len_max = df1['first_list_len'].max()
It gives me the maximum number of columns I need add in data frame then I manually added 4 columns like below.它给了我需要在数据框中添加的最大列数,然后我手动添加了 4 列,如下所示。
df1[['1_','2_','3_','4_']] = pd.DataFrame(df1.first_list.values.tolist(), index= df1.index)
Here I need some automated method to add numbers of columns using first_list_len_max
.在这里,我需要一些自动方法来使用
first_list_len_max
添加列first_list_len_max
。
Remark : first_list_len_max
value may change each time.备注:
first_list_len_max
值可能每次都改变。
Thanks in advance!!提前致谢!!
Use list comprehension with f-string
s:对
f-string
使用列表理解:
first_list_len_max = df1['first_list_len'].max()
L = [f'{i}_' for i in range(1, first_list_len_max + 1)]
df1[L] = pd.DataFrame(df1.first_list.values.tolist(), index= df1.index)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.