1

df is original DataFrame, csv file.

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

This is table a.

enter image description here

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?

0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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']

1 Comment

when i tring panda.DataFrame parameters as an exercise, i found this. Then, confused. And i cant understand ? I just do this step by step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.