Pandas: Adding New Column to Dataframe Which Is a Copy of the Index Column

Pandas: Adding new column to dataframe which is a copy of the index column

I think you need reset_index:

df3 = df3.reset_index()

Possible solution, but I think inplace is not good practice, check this and this:

df3.reset_index(inplace=True)

But if you need new column, use:

df3['new'] = df3.index

I think you can read_csv better:

df = pd.read_csv('university2.csv', 
sep=";",
skiprows=1,
index_col='YYYY-MO-DD HH-MI-SS_SSS',
parse_dates='YYYY-MO-DD HH-MI-SS_SSS') #if doesnt work, use pd.to_datetime

And then omit:

#Changing datetime
df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'],
format='%Y-%m-%d %H:%M:%S:%f')
#Set index from column
df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')

Pandas copy index to new column

There's nothing wrong with how you are setting the temp_index column in the last line. The issue is as it says in the warning. What are you actually trying to achieve? To avoid this warning do newdf = df[df.col2 >= 3].copy(). Note you are indexing with a Boolean key which, AFAIK, creates a copy anyway so the above will not increase your memory footprint.
If you actually want to insert the index to df but only to a subset of the rows try

key = df.col2 >= 3
df = df.loc[key, 'temp_index'] = df.index[key]

Copy index column as a new column

It is Series, so use Series.reset_index first for DataFrame:

df1 = df.reset_index(name='val')
df1['index'] = df1.index

Or create Multiindex Series first and then converting to DataFrame:

df.index = pd.MultiIndex.from_arrays([df.index,df.index], names=('index','time')) 

df1 = df.reset_index(name='val')

Pandas (python): How to add column to dataframe for index?

How about this:

from pandas import *

idx = Int64Index([171, 174, 173])
df = DataFrame(index = idx, data =([1,2,3]))
print df

It gives me:

     0
171 1
174 2
173 3

Is this what you are looking for?

Python Pandas dataframe - add a new column based on index value

As the key of your dictionnary are index of your dataFrame you can try:

d = {'TWOU': '2U',
'MMM': '3M',
'ABT': 'Abbott Laboratories',
'ABBV': 'AbbVie Inc.',
'ABMD': 'Abiomed',
'ACHC': 'Acadia Healthcare',
'ACN': 'Accenture',
'ATVI': 'Activision Blizzard',
'AYI': 'Acuity Brands',
'ADNT': 'Adient',}

df['stock name'] = pd.Series(d)

Correct way to set new column in pandas DataFrame to avoid SettingWithCopyWarning

As it says in the error, try using .loc[row_indexer,col_indexer] to create the new column.

netc.loc[:,"DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM.

Notes

By the Pandas Indexing Docs your code should work.

netc["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

gets translated to

netc.__setitem__('DeltaAMPP', netc.LOAD_AM - netc.VPP12_AM)

Which should have predictable behaviour. The SettingWithCopyWarning is only there to warn users of unexpected behaviour during chained assignment (which is not what you're doing). However, as mentioned in the docs,

Sometimes a SettingWithCopy warning will arise at times when there’s no obvious chained indexing going on. These are the bugs that SettingWithCopy is designed to catch! Pandas is probably trying to warn you that you’ve done this:

The docs then go on to give an example of when one might get that error even when it's not expected. So I can't tell why that's happening without more context.

How to convert index of a pandas dataframe into a column

either:

df['index1'] = df.index

or, .reset_index:

df = df.reset_index(level=0)

so, if you have a multi-index frame with 3 levels of index, like:

>>> df
val
tick tag obs
2016-02-26 C 2 0.0139
2016-02-27 A 2 0.5577
2016-02-28 C 6 0.0303

and you want to convert the 1st (tick) and 3rd (obs) levels in the index into columns, you would do:

>>> df.reset_index(level=['tick', 'obs'])
tick obs val
tag
C 2016-02-26 2 0.0139
A 2016-02-27 2 0.5577
C 2016-02-28 6 0.0303


Related Topics



Leave a reply



Submit