簡體   English   中英

Python 3.x-使用來自另一個數據框的列名創建數據框

[英]Python 3.x - Create dataframe with column names from another dataframe

數據框df1包含具有列名稱的字段“列標題”。 我想創建另一個僅包含df1“列標題”列中列標題的數據框df2。

print(df1['Column header'])
>>
0                                              % Female
1                                  % Below poverty line
2                                    % Rural population
3                      Decadal Population Growth (in %)
4     Availability of Drinking Water Source Within P...
5                                 Concrete Roofs (in %)
6                        Houses With Electricity (in %)
7                        Houses With Televisions (in %)
8                           With Computer/Laptop (in %)
9        Houses With Phones (Telephone + Mobile) (in %)
10                        Houses With 2 wheelers (in %)
11                              Houses With cars (in %)
12              Households With Banking Services (in %)
13                                 Literacy Rate (in %)
14                         Literacy Rate (Rural) (in %)
15                         Literacy Rate (Urban) (in %)
16                  Decadal Difference In Literacy Rate
17                 Student: Teacher Ratio - All Schools
18                     Student: Teacher Ratio - Primary
19               Student: Teacher Ratio - Upper Primary
20     Under-five Mortality Rate (Per 1000 live Births)
21           No of Dispensaries per 1,00,000 population
22                No of Doctors per 1,00,000 population
23    Total patients registered for tuberculosis tre...
24                   Sex Ratio (Females Per 1000 Males)
25                                        Agri GSDP (%)
26                                    Industry GSDP (%)
27                                     Service GSDP (%)
28                        Unemployment Rate   (2011-12)
29                   Rural Unemployment Rate  (2011-12)
30                   Urban Unemployment Rate  (2011-12)
31                Per Capita Public Expenditure (in Rs)
32               Per Capita Private Expenditure (in Rs)
33                          Infant Mortality Rate (IMR)
34                             Maternal Mortality Rate 
35          Coverage Of National Highways (Total in km)
36             Coverage Of State Highways (Total in km)
37                Coverage Of Rural Roads (Total in km)
38                Coverage Of Urban Roads (Total in km)
39                       Railway Coverage (Total in km)
40    Tele-Density [Total Connections /  Total Popul...
Name: Column headers, dtype: object

我想創建包含40列的數據框df2,如上所述。 此數據框中的行將由其他函數填充。 我嘗試如下創建df2-

df2 = pd.DataFrame()  #Creating an empty dataframe 
df2.columns = df1['Column header']
>>
ValueError: Length mismatch: Expected axis has 0 elements, new values have 41 elements

是否可以在Pandas中創建一個空白數據框並隨后指定列名稱?

嘗試這個:

df2 = pd.DataFrame(columns=df1['Column header'])

但您不應創建空的DF,因為將它們逐行填充很慢。 因此,您應該首先收集數據,然后使用預先收集的數據創建DF。

以下是創建帶有自定義列的空數據框的方法:

// Example dataframe
df1 = pd.DataFrame({"Headers": ["Alpha","Beta", "Gama", "Delta"]]}, columns=["Headers"], index=range(4))

print(df1)
//      Headers
//   0  Alpha
//   1  Beta
//   2  Gama
//   3  Delta


print(df1['Headers'].values)
// ['Alpha' 'Beta' 'Gama' 'Delta']

// Make empty dataframe, key here is index=None
df2 = pd.DataFrame({}, columns=df1['Headers'].values, index=None)

print(df2)    
// Empty DataFrame
// Columns: [Alpha, Beta, Gama, Delta]
// Index: []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM