简体   繁体   中英

Python - Pandas - Copy column names to new dataframe without bringing data

I have two dataframes, one called 'data' and other called 'output_data'. 'data' has some info that looks like:

Name 5:10 6:10 7:10
John   5    7   9
Matt   3    4   7
Bill   8    2   3

What I want in output_data is:

Name 5:10 6:10 7:10
John          
Matt          
Bill          

I'll then be programmatically updating each of those cell values based on a calculation from the value in 'data'. I've been attempting to start with just getting all of the column names copied using this code:

#copy column headers
headers = data.columns
data.rename(index = headers, inplace = True)

my understanding with rename() is that it accepts an index an argument but I keep getting an error stating:

'Index' object is not callable

I'm pretty new to pandas and python in general so this is probably something easy and I'm just missing a core concept somewhere. I did try searching for an answer but either it did not quite address what I'm looking for, or maybe I didn't understand the solution. Any help is greatly appreciated.

edit: to give more context, the new dataframe values are going to be a rate of change that will have more values added over time. So the final sheet will be:

Name 5:10 6:10 7:10
John   0% +40% +28%    
Matt   0% +33% +75%     
Bill   0% -75% +50%

it will also ultimately have a few thousand entries as more data is added over time. So I'll end up with one dataframe that contains all of the raw data, and another that has the percent change from hour to hour

You could do it like this:

new_df = df.copy()
new_df[['5:10', '6:10', '7:10']] = ''

or more concise:

new_df = df.copy()
new_df[new_df.columns[1:]] = ''

But why not just create a new dataframe with new_df = df.copy() and then perform your computations without blanking the dataframe? I don't think you need to do that, and it just adds time to the process.

Pandas Copy Column Names From One Data Frame To Another

Use the syntax DataFrame[[*column_names]] to return a new DataFrame with the desired columns from DataFrame. Call pandas.DataFrame.copy() with the new DataFrame as DataFrame to return a deep-copy of DataFrame.

https://www.kite.com/python/answers/how-to-copy-columns-to-a-new-pandas-dataframe-in-python

import pandas as pd
df = pd.DataFrame({"colA":[33,1,71],"colB":[75,52,7],"colC":[22,95,68]})
x = df['largest2'] = df.apply(lambda x: x.nlargest(2).iloc[1], axis=1)

#print(x)
col_names = list(df)
#for col_name in col_names:
#print(col_names)

selected_columns = col_names
new_df = selected_columns.copy()
print(new_df)

从一个数据框到另一个数据框的列名

Create a new dataframe with only specific data

old_data_frame= ....OLD_DATA_FRAME_CONSTRUCTION....
new_dataFrame = pd.DataFrame(columns=old_data_frame.columns, index=old_data_frame.index)

Only to mention all cells will be placed with NaN values

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