[英]Create a new map column from existing columns using python pandas
I have a pandas dataframe that has variable number of columns like C1, C2, C3, F1, F2... F100.我有一个 Pandas 数据框,它具有可变数量的列,如 C1、C2、C3、F1、F2...F100。 I need combine F1, F2 .. F100 into one column of dict/map data type as follows.我需要将 F1, F2 .. F100 组合成一列 dict/map 数据类型,如下所示。 How can I do it using pandas?我如何使用熊猫来做到这一点? C1, C2, C3 are fixed name columns while F1, F2, F100 are variable. C1、C2、C3 是固定名称列,而 F1、F2、F100 是可变的。
Input:输入:
C1 C2 C3 F1 F2 F100
"1" "2" "3" "1" "2" "100"
Output:输出:
C1 C2 C3 Features
"1" "2" "3" {"F1":"1", "F2":"2", "F100": "100"}
filter
+ to_dict
filter
+ to_dict
df['Features'] = df.filter(like='F').to_dict('records')
df
输出: df
C1 C2 C3 C4 F1 F2 F3 F4 Features
0 1 2 3 4 5 6 7 8 {'F1': '5', 'F2': '6', 'F3': '7', 'F4': '8'}
1 x y z w r e s t {'F1': 'r', 'F2': 'e', 'F3': 's', 'F4': 't'}
2 a b c d d f g h {'F1': 'd', 'F2': 'f', 'F3': 'g', 'F4': 'h'}
If you use pandas, you can use df.apply()
function doing so.如果您使用熊猫,您可以使用df.apply()
函数这样做。
Code would be like:代码如下:
def merge(row):
result = {}
for idx in row.index:
if idx.startswith('F'):
result[idx] = row[idx]
print(result)
return result
df['FEATURE'] = df.apply(lambda x: merge(x), axis=1)
Results:结果:
C1 C2 C3 F1 F2 F100 FEATURE
0 1 2 3 1 2 100 {'F1': 1, 'F100': 100, 'F2': 2}
1 11 21 31 11 21 1001 {'F1': 11, 'F100': 1001, 'F2': 21}
2 12 22 32 2 22 2002 {'F1': 2, 'F100': 2002, 'F2': 22}
Consider the following example.考虑以下示例。
d = pd.DataFrame([list('12345678'), list('xyzwrest'), list('abcddfgh')], columns = 'C1, C2, C3, C4, F1, F2, F3, F4'.split(', '))
d
>>> C1 C2 C3 C4 F1 F2 F3 F4
0 1 2 3 4 5 6 7 8
1 x y z w r e s t
2 a b c d d f g h
Let us define the Features
column as follows:让我们定义Features
列如下:
d['Features'] = d.apply(lambda row: {feat: val for feat, val in row.items() if feat.startswith('F')}, axis =1)
#so that when we call d the results will be
d
>>> C1 C2 C3 C4 F1 F2 F3 F4 Features
0 1 2 3 4 5 6 7 8 {'F1': '5', 'F2': '6', 'F3': '7', 'F4': '8'}
1 x y z w r e s t {'F1': 'r', 'F2': 'e', 'F3': 's', 'F4': 't'}
2 a b c d d f g h {'F1': 'd', 'F2': 'f', 'F3': 'g', 'F4': 'h'}
I hope this helps.我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.