-
慕少森
您可以使用 Pandas MultiIndex:https ://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html例如,(示例简单地改编自文档)col_names = [['', 'Cumulative mean', 'Cumulative mean', 'Cumulative mean'],['error', 'days', 'hour', 'minute']]col_tuples = list(zip(*col_names))index = pd.MultiIndex.from_tuples(col_tuples)# use random numberslistsForDataframe = np.array([ np.random.normal(size=4), #list1 np.random.normal(size=4), #list2 np.random.normal(size=4), #list3 np.random.normal(size=4) #list4])# create the dataframe from lists like you did from the comment# include the multiindex objectpd.DataFrame(listsForDataframe.T,columns=index)结果: Cumulative mean error days hour minute0 0.008628 0.037006 -0.805627 -1.9518041 0.527004 0.767902 -1.118312 -0.6598922 0.453782 0.589880 -0.131054 -1.1398023 -1.829740 -0.363859 1.133080 0.784958通过“累积平均值”多列子集然后给出print(d[['Cumulative mean']]): Cumulative mean days hour minute0 0.037006 -0.805627 -1.9518041 0.767902 -1.118312 -0.6598922 0.589880 -0.131054 -1.1398023 -0.363859 1.133080 0.784958
-
墨色风雨
我明白你的意思,发布一个虚拟情况:考虑以下数据框:df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=['a','cum_a','cum_b'])print(df) a cum_a cum_b0 1 2 31 4 5 62 7 8 9我们的目标是使用模式更改列,例如cum_a,cum_b。这可以通过使用来完成df.filter():values_to_rename=['change1','change2'] #sequential list of values to replaced=dict(zip(df.filter(like='cum').columns,values_to_rename)) #create a dict#{'cum_a': 'change1', 'cum_b': 'change2'}df=df.rename(columns=d)print(df) a change1 change20 1 2 31 4 5 62 7 8 9
-
POPMUISE
如果可以,请尝试这样做import pandas as pdimport numpy as npdf = {'col_1': [0, 1, 2, 3], 'col_2': [4, 5, 6, 7]}df = pd.DataFrame(df)df[[ 'column_new_1', 'column_new_2','column_new_3']] = [np.nan, 'dogs',3]这可能可以解决问题或者您可以尝试使用此示例import pandas as pdimport numpy as npdf = pd.DataFrame({'col_1': [0, 1, 2, 3],'col_2': [4, 5, 6, 7] })这些是您可以使用的示例,但当然您需要自己添加数据。希望这有助于创建多行列