logo
Tags down

shadow

Pandas Column Names Not Lining Up When .dat File Read


By : user2176374
Date : October 14 2020, 02:21 PM
With these it helps try adding encoding='UTF-16' to pd.read_table()
(Sorry, not enough reputation to add a comment.)
code :


Share : facebook icon twitter icon

Read url as pandas dataframe with column names (python3)


By : user6589949
Date : March 29 2020, 07:55 AM
Does that help The link you provided was missing a hyphen. I've corrected that in my answer. Basically you need to decode the s string into utf-8, then split it on \n to get each row and then split each row on white space to get each value separately. This will give you a nested list representation of the data set which you can convert to a pandas dataframe and thereafter you can assign the column names.
code :
import pandas as pd
import io
import requests
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/heart/heart.dat"
s = requests.get(url).content
s = s.decode('utf-8')
s_rows = s.split('\n')
s_rows_cols = [each.split() for each in s_rows]
header_row = ['age','sex','chestpain','restBP','chol','sugar','ecg','maxhr','angina','dep','exercise','fluor','thal','diagnosis']
c = pd.DataFrame(s_rows_cols, columns = header_row)
c.head()

using pandas to read a csv file with whatever columns matchi with the column names given in a list


By : Muhammed Boz
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can use intersection of real columns names with list:
code :
df = pd.read_csv('file.csv')
df1 = df[df.columns.intersection([mylist])]

Use pandas to read in text file with row as column names


By : mango dai
Date : March 29 2020, 07:55 AM
To fix this issue I'm working on a project to read in a text file of variable length which will be generated by a user. There are several comments at the beginning of the text file, one of which needs to be used as the column name. I know it is possible to do this with genfromtxt(), but I am required to use pandas. Here is the beginning of a sample text file: , One way may be to try following:
code :
df = pd.read_csv('example.txt', sep='\s+', engine='python', header=2)

# the first column name become #a so, replacing the column name
df.rename(columns={'#a':'a'}, inplace=True)

# alternatively, other way is to replace # from all the column names
#df.columns = [column_name.replace('#', '') for column_name in df.columns]
print(df)
   a  b  c  d  f  g  h  i  j   k ...   p   q   r   s   t   v   w   x   y   z
0  0  1  2  3  4  5  6  7  8   9 ...  13  14  15  16  17  18  19  20  21  22
1  1  2  3  4  5  6  7  8  9  10 ...  14  15  16  17  18  19  20  21  22  23

[2 rows x 23 columns]

Add column names to data read from csv file without column names


By : Coco
Date : March 29 2020, 07:55 AM
help you fix your problem I am using Apache Spark with Scala. , You can use toDF to specify column names when reading the CSV file:
code :
val df = spark.read.option("inferSchema","true").csv("../myfile.csv").toDF(
  "ID", "name", "age", "numOfFriends"
)
val newColNames = Seq("ID", "name", "age", "numOfFriends")
val df2 = df.toDF(newColNames: _*)

Read csv file and split in columns keeping column names. Pandas


By : fitrazh
Date : March 29 2020, 07:55 AM
seems to work fine When i import csv file with ";" separator and then split columns, they appear without original names but indexed. , Use split for original columns:
code :
data= pd.read_csv('path')
df = data.iloc[:,0].str.split(';', expand=True)
df.columns = data.columns[0].split('; ')
print (df)
   code units price
0  4017   142    20
1   808    76    15
2   316    39     7
3   209    27    45
4   344    14    32
data = pd.read_csv('path', sep=";", quoting=3)
data.columns = data.columns.str.strip('" ')
data.iloc[:, [0, -1]] = data.iloc[:, [0, -1]].apply(lambda x: x.str.strip('"'))
Related Posts Related Posts :
  • Running sums from one column conditional on values in another column
  • How to create dictionary combinations? Costliest car problem
  • Select each letter in a file, then find its ASCII value (Python)
  • Python: logarithm to base 2 in nested lists
  • Knapsack algorithm, weird behavior (python3)
  • How to use the break between lines in a txt file to start a new list in Python 3
  • Default *key* when accessing dictionary?
  • "TypeError: a bytes-like object is required, not 'str'" during OAuth 2.0 callback request
  • Lat Long won't show on Heatmap with ipyleaflet
  • Python Flask paginate error:paginated page returned 404
  • can't pickle _thread.RLock objects when running tune of ray packge for python (hyper parameter tuning)
  • Python 3: Positions of button and input text in a text box
  • Bijective function between two nested lists (Python)
  • How to get python console logs on my tkinter window instead of a CMD window while executing an exe file created using py
  • how combine '"+variable+"' in python 3
  • Year wise cumsum based on condition on other column Python 3+ Pandas data frame
  • How to send a message with discord.py from outside the event loop (i.e. from python-telegram-bot thread)?
  • BytesIO replaces transparency in PNG files with black background
  • Cannot resize and save image of type .tif after patch extraction with tf
  • Adding an Image to the python GUI from within the script
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org