简体   繁体   English

使用python pandas在特定列中使用最大值在数据框中添加新的“x”列数

[英]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.

相关问题 熊猫-python-使用列将值添加到新列 - pandas - python - using columns to add value to new column 计算 Pandas 中整个数据帧中特定字符串的数量,并将其值添加到新列中 - count number of a specific string in entire data frame in Pandas and add its value in a new column 使用 Pandas 从现有列创建新列到数据框 - Create a new column to data frame from existing columns using Pandas 使用熊猫更改数据框的特定列 - change specific columns of data frame using pandas Pandas(python):列中的max定义新列中的新值 - Pandas (python): max in columns define new value in new column 使用pandas / python在数据框中出现列值小于某个数字时删除行? - Remove rows when the occurrence of a column value in the data frame is less than a certain number using pandas/python? 使用 else if 逻辑将条件列添加到 Pandas 数据框 - Python - Add conditional column to Pandas Data Frame using else if logic - Python Python:使用其他列将Pandas中的新列的值分配为列表 - Python: Assign value to a new column in Pandas as list using other columns Pandas:比较数据框的列并根据条件添加新列和值 - Pandas : Compare the columns of a data frame and add a new column & value based on a condition 如何使用 pandas 数据框将数据框的每一列值添加到一张一张的新工作表中 - How to add each column of a data frame values in one by one new sheets using pandas data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM