使用if语句重建pandas Dataframe [英] Rebuild pandas Dataframe with if Statements
问题描述
正如在另一个问题中已经问到的那样,标题为"Rebuild pandas Dataframe".我还有关于继续使用更多列的一些问题.
As allready asked in another Question with the Title "Rebuild pandas Dataframe" i still have some Questions about going on with even more columns.
情况: 我有一个带有4列的数据框,这些列中的值是相当随机的.如以下示例:
Situation: I have a Dataframe with 4 Columns, the Values inside the Columns are pretty Random. Such as this example:
df = pd.DataFrame({'col1': ['id 1', 'id 2', 'test 3', 'test 4'],
'col2': ['test 1', 'test 2',
'ne 5261', 'id 4'],
'col3': ['Number 12344', 'Number 21612','id 3','Number 1131'],
'col4':['ne 315','Number 1264777','ne 1415','ne 52']})
我的目标是拥有一个数据框,其中每个Col仅具有以Same子字符串开头的值,例如以下示例:
My Goal is to have a Dataframe in which each Col has only the Values beginning with the Same substring like this example:
下面的代码(已从最后一个问题"致谢:@AndrejKesely)变为了以下三列的有用代码:
What already worked for 3 columns is the following code (from The last Question credits to: @AndrejKesely):
def key_fn(x):
if 'id' in x:
return 0
if 'test' in x:
return 1
if 'Number' in x:
return 2
return 3
df = pd.DataFrame([sorted(l, key=key_fn) for l in df.values], columns=df.columns)
print(df)
由于我现在有4个列,因此我向函数添加了另一个if语句,如下所示:
Since i now have 4 cols i added another if Statement to the Function, looking as follows:
def key_fn(x):
if 'id' in x:
return 0
if 'test' in x:
return 1
if 'Number' in x:
return 2
if 'ne' in x:
return 3
return 4
df = pd.DataFrame([sorted(l, key=key_fn) for l in df.values], columns=df.columns)
这给了我以下输出:
这是一个小例子,当我了解它是如何工作的时,我需要将其应用于总共17列. 预先感谢您的帮助!
This is a small example, when i understand how it works i need to apply it to a Total of 17 Columns. Thank you in advance for your Help!
推荐答案
df = pd.DataFrame({'col1': ['id 1', 'id 2', 'test 3', 'test 4'],
'col2': ['test 1', 'test 2',
'ne 5261', 'id 4'],
'col3': ['Number 12344', 'Number 21612','id 3','Number 1131'],
'col4':['ne 315','Number 1264777','ne 1415','ne 52']})
def key_fn(x):
if 'id' in x:
return 0
if 'test' in x:
return 1
if 'Number' in x:
return 2
if 'ne' in x:
return 3
return 4
out_df = pd.DataFrame(np.array(sorted(np.ravel(df.values), key=key_fn)).reshape(df.shape), columns=df.columns).T
print(out_df)
打印:
col1 col2 col3 col4
0 id 1 test 1 Number 12344 ne 315
1 id 2 test 2 Number 21612 ne 5261
2 id 3 test 3 Number 1264777 ne 1415
3 id 4 test 4 Number 1131 ne 52
这篇关于使用if语句重建pandas Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!