How to make the column of one file as heading of other file's column with pandas in python? - TagMerge
4How to make the column of one file as heading of other file's column with pandas in python?How to make the column of one file as heading of other file's column with pandas in python?

How to make the column of one file as heading of other file's column with pandas in python?

Asked 1 years ago
1
4 answers

You need to use your heading variable while printing. So, instead of using column {[i]}, you need to use {heading.loc[i,0]}.

Here is the full code:

import pandas as pd
from scipy.stats import spearmanr

data = pd.DataFrame([[2, 3, 5, 10, 76],
                     [20, 6, 10, 100, 87],
                     [40, 30, 15, 1000, 46],
                     [60, 9, 20, 10000, 43],
                     [80, 12, 25, 100000, 98]])

vector = pd.DataFrame([[60], [80], [100], [120], [140]])
heading = pd.DataFrame([['red'], ['orange'], ['green'], ['blue'], ['yellow']])

for i,j in data.iteritems():
    col=j
    coef, p = spearmanr(col, vector)
    alpha = 0.05
    if p < alpha:
        print('\n\nSpearmans correlation coefficient: %.3f' % coef)
        print(f' {heading.loc[i,0]} method are correlated (reject H0) p=%.3f' % p)

where it produces the following output:

Spearmans correlation coefficient: 1.000
 red method are correlated (reject H0) p=0.000


Spearmans correlation coefficient: 1.000
 green method are correlated (reject H0) p=0.000


Spearmans correlation coefficient: 1.000
 blue method are correlated (reject H0) p=0.000

Source: link

0

In order to read a csv in that doesn't have a header and for only certain columns you need to pass params header=None and usecols=[3,6] for the 4th and 7th columns:
df = pd.read_csv(file_path, header=None, usecols=[3,6])
Use usecols and names parameters
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'])
or use header=None to explicitly tells people that the csv has no headers (anyway both lines are identical)
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'], header=None)
So that you can retrieve your data by
# with `names` parameter
df['colA']
df['colB']
instead of
# without `names` parameter
df[0]
df[1]

Source: link

0

HTML Table Syntax:
<table>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
  </tr>
  <tr>
    <td>Cell 4</td>
    <td>Cell 5</td>
    <td>Cell 6</td>
  </tr>
</table>
An example with use of <th>
<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email Address</th>
  </tr>
  <tr>
   <td>Hillary</td>
   <td>Nyakundi</td>
   <td>tables@mail.com</td>
  </tr>
  <tr>
    <td>Lary</td>
    <td>Mak</td>
    <td>developer@mail.com</td>
  </tr>
</table>
Caption Syntax
<table>
  <caption></caption>
  <tr> </tr>
</table>
An example with use of <caption>
<table>
  <caption>Free Coding Resources</caption>
  <tr>
    <th>Sites</th>
    <th>Youtube Channels</th>
    <th>Mobile Appss</th>
  </tr>
  <tr>
    <td>Freecode Camp</td>
    <td>Freecode Camp</td>
    <td>Enki</td>
  </tr>
  <tr>
    <td>W3Schools</td>
    <td>Academind</td>
    <td>Programming Hero</td>
  </tr>
  <tr>
    <td>Khan Academy</td>
    <td>The Coding Train</td>
    <td>Solo learn</td>
  </tr>
</table>
Scope Syntax
<table>
 <tr>
   <th scope="value">
 </tr>
</table

Source: link

0

<style>.lazyload-placeholder { display: none; }</style>
$ python pip install pandas
If you're using Linux or MacOS:
$ pip install pandas
Note that you may get a ModuleNotFoundError or ImportError error when running the code in this article. For example:
ModuleNotFoundError: No module named 'openpyxl'
If this is the case, then you'll need to install the missing module(s):
$ pip install openpyxl xlsxwriter xlrd
First, let's import the Pandas module:
import pandas as pd

Source: link

Recent Questions on python

    Programming Languages