我有这个人。数据帧:
a_name Season yl
yl
4.939 cherka 2000.0 [4.939]
4.441 cherka 2001.0 [4.441]
4.320 cherka 2002.0 [4.32]
3.718 cherka 2003.0 [3.718]
4.533 cherka 2004.0 [4.533]
如何将其转换为:
a_name Season yl
yl
4.939 cherka 2000.0 4.939
4.441 cherka 2001.0 4.441
4.320 cherka 2002.0 4.32
3.718 cherka 2003.0 3.718
4.533 cherka 2004.0 4.533
我做到了:
df.groupby(['a_name', 'Season', 'yl'])['yl'].unique().reset_index(level=[0,1])
答案 0 :(得分:1)
使用numpy:
df["y1"] = np.vstack(df["y1"])
答案 1 :(得分:1)
df['max']=df['max'].apply(pd.Series)
df
Out[1428]:
idCaseMax idCaseMin lineId max min
0 5 10 1 120 -110
1 27 23 2 150 -205
2 15 40 3 110 -80
3 11 8 4 90 -150
数据输入
df = pd.DataFrame({"lineId":[1,2,3,4], "idCaseMin": [10, 23, 40, 8], "min": [-110, -205, -80, -150], "idCaseMax": [5, 27, 15, 11], "max": [[120], [150], [110], [90]]})
答案 2 :(得分:1)
扩展我的评论,使用.str[0]
:
df
a_name Season yl
yl
4.939 cherka 2000.0 [4.939]
4.441 cherka 2001.0 [4.441]
4.320 cherka 2002.0 [4.32]
3.718 cherka 2003.0 [3.718]
4.533 cherka 2004.0 [4.533]
df['yl'] = df['yl'].str[0]
df
a_name Season yl
yl
4.939 cherka 2000.0 4.939
4.441 cherka 2001.0 4.441
4.320 cherka 2002.0 4.320
3.718 cherka 2003.0 3.718
4.533 cherka 2004.0 4.533
您可以选择使用assign
(piRsquared's suggestion)创建副本:
df_new = df.assign(df['yl'].str[0])
df_new
a_name Season yl
yl
4.939 cherka 2000.0 4.939
4.441 cherka 2001.0 4.441
4.320 cherka 2002.0 4.320
3.718 cherka 2003.0 3.718
4.533 cherka 2004.0 4.533