Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

python - Pandas column creation

I'm struggling to understand the concept behind column naming conventions, given that one of the following attempts to create a new column appears to fail:

from numpy.random import randn
import pandas as pd

df = pd.DataFrame({'a':range(0,10,2), 'c':range(0,1000,200)},
columns=list('ac'))
df['b'] = 10*df.a
df

gives the following result:

enter image description here

Yet, if I were to try to create column b by substituting with the following line, there is no error message, yet the dataframe df remains with only the columns a and c.

df.b = 10*df.a   ### rather than the previous df['b'] = 10*df.a ###

What has pandas done and why is my command incorrect?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you did was add an attribute b to your df:

In [70]:
df.b = 10*df.a 
df.b

Out[70]:
0     0
1    20
2    40
3    60
4    80
Name: a, dtype: int32

but we see that no new column has been added:

In [73]:    
df.columns

Out[73]:
Index(['a', 'c'], dtype='object')

which means we get a KeyError if we tried df['b'], to avoid this ambiguity you should always use square brackets when assigning.

for instance if you had a column named index or sum or max then doing df.index would return the index and not the index column, and similarly df.sum and df.max would screw up those df methods.

I strongly advise to always use square brackets, it avoids any ambiguity and the latest ipython is able to resolve column names using square brackets. It's also useful to think of a dataframe as a dict of series in which it makes sense to use square brackets for assigning and returning a column


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...