我已经在Python中创建了一个数据框,并使用info()函数查看其元数据。但是无法将info()结果存储到数据帧中,有什么方法可以实现。
df2 = pd.DataFrame ({'A': pd.Series(range(1,4)),
'B' : pd.Timestamp('20181017'),
'C' : pd.Series(1, index=list(range(4)), dtype = 'float32'),
'D' : np.array([3] * 4, dtype = 'int32'),
'E' : pd.Categorical(["test", "train", "test", "train"]),
'F' : 'foo'})
df2.info() #want its output in dataframe.
a = pd.DataFrame(df2.info())
a
''''
答案 0 :(得分:2)
IIUC:
buffer = pd.compat.StringIO()
df2.info(buf=buffer)
s = buffer.getvalue()
df=pd.DataFrame(s.split("\n"),columns=['info'])
print(df)
info
0 <class 'pandas.core.frame.DataFrame'>
1 Int64Index: 4 entries, 0 to 3
2 Data columns (total 6 columns):
3 A 3 non-null float64
4 B 4 non-null datetime64[ns]
5 C 4 non-null float32
6 D 4 non-null int32
7 E 4 non-null category
8 F 4 non-null object
9 dtypes: category(1), datetime64[ns](1), float3...
10 memory usage: 260.0+ bytes
一旦有了数据框,您就可以随时进行切片和修改,例如:
df_info=df.loc[3:8,'info'].str.split(n=1,expand=True).reset_index(drop=True)
print(df_info)
0 1
0 A 3 non-null float64
1 B 4 non-null datetime64[ns]
2 C 4 non-null float32
3 D 4 non-null int32
4 E 4 non-null category
5 F 4 non-null object