[英]How to change column names in pandas Dataframe using a list of names?
[英]How to turn a list of values into column names and inputted as a variable in a pandas dataframe?
我想将值列表(定义为modernization_area)转换为列标题。 例如,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)
帮助你并不容易,因为你的问题缺乏很多信息。 我假设 hipotheticals keyword_table
和report_table
。 其实,我不知道我是否真的得到了你真正想要的。 但我希望这段代码可以帮助:
假设块:
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
现在,在假设之后,您可以执行以下操作:
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.