我有一个数据框如下
empIdn date new
517 2019-05-02 [12:00:12, 12:32:05, 05:18:57, 05:23:02]
我想将 new
列中的值拆分为各个列。
像这样的东西
empIdn date time_0 time_1 time_2 time_3
517 2019-05-02 12:00:12 12:32:05 05:18:57 05:23:02
我能够做到这一点
df = df.join(pd.DataFrame(df3.new.values.tolist(), df.index).add_prefix('time_'))
df.drop('new', axis=1, inplace = True)
我的问题:
有没有办法让自动生成的名称变成字母(A、B、C..)而不是数字?
删除列
new
的行可以集成到第一行本身吗?
最佳答案
使用rename
将整数转换为大写字母,并使用DataFrame.pop
删除new
:
import string
d = dict(enumerate(list(string.ascii_uppercase)))
#print (d)
df = df3.join(pd.DataFrame(df3.pop('new').values.tolist(),
index=df3.index)
.rename(columns=d)
.add_prefix('time_'))
print (df)
empIdn date time_A time_B time_C time_D
0 517 2019-05-02 12:00:12 12:32:05 05:18:57 05:23:02
关于python - 自动生成的名称为字母而不是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702809/