Python 自动生成的名称为字母而不是数字
我有一个如下的数据帧Python 自动生成的名称为字母而不是数字,python,pandas,Python,Pandas,我有一个如下的数据帧 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 我
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)
我的问题是:
new
是否可以集成到第一行中请尝试下面的代码。我希望这会有所帮助 如果您试图生成列名而不是time\u 0到time\u A,那么您没有生成它,您正在传递
df.index
,它是RangeIndex(start=0,stop=1,step=1),例如(0,1,2,3),然后添加后缀使其成为time\
现在,您的问题是将列名生成为time_A、time_B、time_C和time_D
它可以使用下面的代码来完成
pd.DataFrame(data.new.values.tolist(),columns=['A'、'B'、'C'、'D'])。添加前缀('time')
对于第二个问题,您可以将drop语句与join一起使用,如下所示:
df = df.join(pd.DataFrame(data.new.values.tolist(), columns=['A','B','C','D']).add_prefix('time_')).drop('new', axis=1)
输出将如下所示:
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
使用rename
将整数转换为大写字母,以及删除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