简体   繁体   English

如何将值列表转换为列名并作为pandas 数据框中的变量输入?

[英]How to turn a list of values into column names and inputted as a variable in a pandas dataframe?

I want to turn a list of values (defined as modernization_area) into column headers.我想将值列表(定义为modernization_area)转换为列标题。 For example, the modernization_area outputs: A, B, C, D and the want the function to loop through each area by generating columns A, B, C, and D. The variable would ideally replace 'modernization_area' in the last line, but python is not accepting that as a variable.例如,modernization_area 输出:A、B、C、D 并且希望函数通过生成 A、B、C 和 D 列来循环遍历每个区域。理想情况下,该变量将替换最后一行中的“modernization_area”,但是python 不接受它作为变量。

modernization_list = pd.DataFrame(keyword_table['Modernization_Area'].unique().tolist())

modernization_list.columns = ['Modernization_Area']

x = range(len(modernization_list['Modernization_Area'].unique().tolist()))

for i in x:

    modernization_area = modernization_list._get_value(i, 'Modernization_Area')

    keyword_subset = keyword_table[keyword_table.Modernization_Area == modernization_area]

    keywords = keyword_subset['Keyword'].tolist()

    report_table['a'] = report_table.award_description.str.findall('({0})'.format('|'.join(keywords), flags=re.IGNORECASE)

It is not easy to help you because your question is lacking a lot of information.帮助你并不容易,因为你的问题缺乏很多信息。 I am assuming hipotheticals keyword_table and report_table .我假设 hipotheticals keyword_tablereport_table Actually, I don't know if I really got what you truly want.其实,我不知道我是否真的得到了你真正想要的。 But I hope this piece of code could help:但我希望这段代码可以帮助:

Block of assumptions:假设块:

supposed_keyword_table = pd.DataFrame({'Keyword': ['word1', 'word2', 'word3', 'word4', 'word5', 'word6', 'word7'], 'Modernization Area': ['A', 'B', 'C', 'D', 'A', 'B', 'D']})

supposed_report_table =  pd.DataFrame({'Modernization Area': ['A', 'B', 'C', 'D'], 'Some Value': [1, 2, 3, 4]})

supposed_keyword_table

    Keyword     Modernization Area
0   word1   A
1   word2   B
2   word3   C
3   word4   D
4   word5   A
5   word6   B
6   word7   D

supposed_report_table

    Modernization Area  Some Value
0   A   1
1   B   2
2   C   3
3   D   4

Now, after assumptions, here is what you can do:现在,在假设之后,您可以执行以下操作:

keyword_table_by_mod_area = supposed_keyword_table.groupby(['Modernization Area'])['Keyword'].apply(lambda x: '|'.join(x))

supposed_report_table = pd.merge(supposed_report_table, keyword_table_by_mod_area, on='Modernization Area', how='left')

supposed_report_table

    Modernization Area  Some Value  Keyword
0   A   1   word1|word5
1   B   2   word2|word6
2   C   3   word3
3   D   4   word4|word7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用名称列表更改 pandas Dataframe 中的列名称? - How to change column names in pandas Dataframe using a list of names? 布尔值到一个列表中的列名,dataframe pandas python - Boolean values to column names in one list, dataframe pandas python 如何将一系列Pandas数据框行变成具有多个值的一列? - How to turn a series of Pandas dataframe rows into one column with multiple values? 如何使用 pandas 中的 dataframe 中的值作为列名,并将列名作为值在 dataframe 中使用 - how to use values from a dataframe in pandas for column names, and column names as values insdie the dataframe 将 Pandas 数据框列表转换为布尔列 - Turn pandas dataframe list into boolean column 如何更新 Pandas Dataframe 中值列表的列 - How to update a column for list of values in Pandas Dataframe 如何对行值进行排序并将其替换为熊猫数据框上的列名 - How to sort rows values and replace them by column names on a pandas dataframe 如何通过数据框值在python pandas中选择列名称? - How to select column names in python pandas by dataframe values? 如何在条件下将列名列表添加到 pandas 中的 DataFrame - How to add a list of column names to a DataFrame in pandas under conditions 如何将 map 字典键列在 Python Pandas ZBA834BA059A9A379459C112E47 中的列名列表中? - How to map dictionary keys to list of column names in Python Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM