我想根据索引中的值创建一个列:
如果索引以字母值开头而不是 'I0',则返回“P”,否则返回“C”。
尝试过:
df['new_col'] = ['P' if (x[0].isalpha() and not x[0].startswith("I0")) else 'C' for x in df.index]
但它返回以以下开头的行的“P” 'I0'
:
A B C new_col
Index
I00001 1.325337 4.692308 1.615385 P
I00002 1.614780 3.615385 0.769231 P
I00003 1.141453 5.461538 2.000000 P
I00004 0.918300 8.538462 2.769231 P
I00005 1.189606 11.846154 2.692308 P
I00006 0.941459 7.153846 2.153846 P
I00007 0.466383 12.153846 9.384615 P
I00008 0.308627 198.692308 23.461538 P
I00011 0.537142 23.384615 6.846154 P
I00012 1.217390 11.923077 1.230769 P
I00013 1.052840 3.384615 2.000000 P
...
可重现的例子:
df = pd.DataFrame({'A': {'I00001': 1.3253365856660808,
'I00002': 1.6147800817881086,
'I00003': 1.1414534979918203,
'I00004': 0.9183004454646491,
'I00005': 1.1896061362142527,
'I00006': 0.941459102789141,
'I00007': 0.46638312473267185,
'I00008': 0.3086270976042302,
'I00011': 0.5371419441302684,
'I00012': 1.2173904641254587,
'I00013': 1.052839529263679,
'I00014': 1.3587324409735149,
'I00015': 3.464101615137755,
'I00016': 1.1989578808281798,
'I00018': 0.2433560755649686,
'I00019': 0.5510000980337852,
'I00020': 3.464101615137755,
'I00022': 1.0454523047666737,
'I00023': 1.3850513878332371,
'I00024': 1.3314720972390754},
'B': {'I00001': 4.6923076923076925,
'I00002': 3.6153846153846154,
'I00003': 5.461538461538462,
'I00004': 8.538461538461538,
'I00005': 11.846153846153847,
'I00006': 7.153846153846154,
'I00007': 12.153846153846153,
'I00008': 198.69230769230768,
'I00011': 23.384615384615383,
'I00012': 11.923076923076923,
'I00013': 3.3846153846153846,
'I00014': 1.0,
)
30秒到达战场
翻阅古今
相关分类