简体   繁体   English

pandas.DataFrame输入DataFrame但得到NaN吗?

[英]pandas.DataFrame input DataFrame but get NaN?

df is original DataFrame, csv file. df是原始的DataFrame,csv文件。

a = df.head(3)                  # get part of df.

This is table a. 这是表a。

在此处输入图片说明

b = a.loc[1:3,'22':'41']        #select part of a.

c = pd.DataFrame(data=b,index=['a','b'],columns=['v','g']) # give index and columns

final 最后

b show 2x2. b显示2x2。 I get four value. 我得到四个价值。

c show 2x2 NaN. c显示2x2 NaN。 I get four NaN. 我得到四个NaN。

why c don't contain any number? 为什么c不包含任何数字?

Try using .values , you are running into 'intrinsic data alignment' 尝试使用.values ,您将.values “内部数据对齐”

c = pd.DataFrame(data=b.values,index=['a','b'],columns=['v','g']) # give index and columns

Pandas likes to align indexes, by converting your 'b' dataframe into a np.array, you can then use the pandas dataframe constructor to build a new dataframe with those 2x2 values assigning new indexing. 熊猫喜欢对齐索引,方法是将“ b”数据帧转换为np.array,然后可以使用熊猫数据帧构造函数构建一个新的数据帧,并使用这些2x2值分配新的索引。

Your DataFrame b already contains row and column indices, so when you try to create DataFrame c and you pass index and columns keyword arguments, you are implicitly indexing out of the original DataFrame b . 您的DataFrame b已经包含行索引和列索引,因此当您尝试创建DataFrame c并传递indexcolumns关键字参数时,您将隐式地从原始DataFrame b索引。

If all you want to do is re-index b , why not do it directly? 如果您要做的只是重新索引b ,为什么不直接做呢?

b = b.copy()
b.index = ['a', 'b']
b.columns = ['v', 'g']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM