Changing the string values in a column Pandas Python - python

I have a pandas csv file down below. In the symbol column I want to replace all the BTC/USD plots to BTCUSD. How would I be able to do that?
Code:
# read_csv function which is used to read the required CSV file
data = pd.read_csv("sample.txt")
csv file:
unix,date,symbol,open,high,low,close,Volume BTC
1544217600,2018-12-07 21:20:00,BTC/USD,3348.77,3350.41,3345.07,3345.12,3.11919918
1544217540,2018-12-07 21:19:00,BTC/USD,3342.24,3351.14,3342.24,3346.37,21.11950697
1544217480,2018-12-07 21:18:00,BTC/USD,3336.02,3336.02,3336.02,3336.02,0.0
1544217420,2018-12-07 21:17:00,BTC/USD,3332.26,3336.02,3330.69,3336.02,3.28495056
Expected Output:
unix,date,symbol,open,high,low,close,Volume BTC
1544217600,2018-12-07 21:20:00,BTCUSD,3348.77,3350.41,3345.07,3345.12,3.11919918
1544217540,2018-12-07 21:19:00,BTCUSD,3342.24,3351.14,3342.24,3346.37,21.11950697
1544217480,2018-12-07 21:18:00,BTCUSD,3336.02,3336.02,3336.02,3336.02,0.0
1544217420,2018-12-07 21:17:00,BTCUSD,3332.26,3336.02,3330.69,3336.02,3.28495056

# importing pandas module
import pandas as pd
# reading csv file
data = pd.read_csv("sample.txt")
# overwriting column with replaced value
data["Team"]= data["symbol"].str.replace("BTC/USD", "BTCUSD", case = False)

You can use str.replace like this:
df['symbol'] = df['symbol'].str.replace('BTC/USD','BTCUSD')

Related

Re-write a json file to add missing data values with Pandas

I am trying to re-write a json file to add missing data values. but i cant seem to get the code to re-write the data on the json file. Here is the code to fill in missing data:
import pandas as pd
import json
data_df = pd.read_json("Data_test.json")
#replacing empty strings with nan
df2 = data_df.mask(data_df == "")
#filling the nan with data from above.
df2["Food_cat"].fillna(method="ffill", inplace=True,)
"Data_test.json" is the file with the list of dictionary and I am trying to either edit this json file or create a new one with the filled in data that was missing.
I have tried using
with open('complete_data', 'w') as f:
json.dump(df2, f)
but it does not seem to work. is there a way to edit the current data or create a new json file with the completed data?
this is the original data, I would like to keep this format.
Try to do this
import pandas as pd
import json
data_df = pd.read_json("Data_test.json")
#replacing empty strings with nan
df2 = data_df.mask(data_df == "")
#filling the nan with data from above.
df2["Food_cat"].fillna(method="ffill", inplace=True,)
df2.to_json('path_of_file.json')
Tell me if it works.

Python, how to add a new column in excel

I am having below file(file1.xlsx) as input. In total i am having 32 columns in this file and almost 2500 rows. Just for example i am mentioning 5 columns in screen print
I want to edit same file with python and want output as (file1.xlsx)
it should be noted i am adding one column named as short and data is a kind of substring upto first decimal of data present in name(A) column of same excel.
Request you to please help
Regards
Kawaljeet
Here is what you need...
import pandas as pd
file_name = "file1.xlsx"
df = pd.read_excel(file_name) #Read Excel file as a DataFrame
df['short'] = df['Name'].str.split(".")[0]
df.to_excel("file1.xlsx")
hello guys i solved the problem with below code:
import pandas as pd
import os
def add_column():
file_name = "cmdb_inuse.xlsx"
os.chmod(file_name, 0o777)
df = pd.read_excel(file_name,) #Read Excel file as a DataFrame
df['short'] = [x.split(".")[0] for x in df['Name']]
df.to_excel("cmdb_inuse.xlsx", index=False)

How to read and query headers of txt using Pandas?

I am writing a script to read txt file using Pandas.
I need to query on particular type of hearders.
Reading excel is possible but i cannot read txt file.
import pandas as pd
#df=pd.read_excel('All.xlsx','Sheet1',dtype={'num1':str},index=False) #works
df=pd.read_csv('read.txt',dtype={'PHONE_NUMBER_1':str}) #doest work
array=['A','C']
a = df['NAME'].isin(array)
b = df[a]
print(b)
try using this syntax.
you are not using the correct key value
df=pd.read_csv('read.txt',dtype={'BRAND_NAME_1':str})
You can try this:
import pandas as pd
df = pd.read_table("input.txt", sep=" ", names=('BRAND_NAME_1'), dtype={'BRAND_NAME_1':str})
You can read file txt then astype for column.
Read file:
pd.read_csv('file.txt', names = ['PHONE_NUMBER_1', 'BRAND_NAME_1'])
names: is name of columns
Assign type:
df['PHONE_NUMBER_1'] = df['PHONE_NUMBER_1'].astype(str)

Print specific columns from an excel file imported to python

I have a table as below:
How can I print all the sources that have an 'X' for a particular column?. For example, if I want to specify "Make", the output should be:
Delivery
Reputation
Profitability
PS: The idea is to import the excel file in python and do this operation.
use pandas
import pandas as pd
filename = "yourexcelfile"
dataframe = pd.read_excel(filename)
frame = dataframe.loc[dataframe["make"] == "X"]
print(frame["source"])

Extract rows based on the particular value from a particular column and export it to new csv file

CSV file as stack.csv
PROBLEM_CODE;OWNER_EMAIL;CALENDAR_YEAR;CALENDAR_QUARTER
CONFIG_ASSISTANCE;dalangle#gmail.com;2014;2014Q3
ERROR_MESSAGES;aganju#gmail.com;2014;2014Q3
PASSWORD_RECOV;dalangle#gmail.com;2014;2014Q3
ERROR_MESSAGES;biyma#gmail.com;2014;2014Q3
ERROR_MESSAGES;derrlee#gmail.com;2014;2014Q3
SOFTWARE_FAILURE;dalangle#gmail.com;2014;2014Q3
ERROR_MESSAGES;maariano#gmail.com;2014;2014Q3
SOFTWARE_FAILURE;dalangle#gmail.com;2014;2014Q3
My Code:
import pandas as pd
import csv
data = pd.read_csv('stack.csv', sep='delimiter')
min_indices = (data['OWNER_EMAIL'] == dalangle#gmail.com)
data = data[min_indices]
data.to_csv('isabevdata.csv')
Error:
KeyError: 'OWNER_EMAIL'
I need help with this code using pandas. I want to remove some columns later on from the result: isabevdata.csv --> using petl module and then send the table to front end for display

Categories

Resources