简体   繁体   中英

pandas.DataFrame input DataFrame but get NaN?

df is original DataFrame, csv file.

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

This is table 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. I get four value.

c show 2x2 NaN. I get four NaN.

why c don't contain any number?

Try using .values , you are running into 'intrinsic data alignment'

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.

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 .

If all you want to do is re-index b , why not do it directly?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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