Pandas:关于使用 pandas 比较和(重新)计算字段的新手问题
Pandas: Newbie question on compare and (re)calculate fields with pandas
我需要做的是比较 csv 文件中一行中的 2 个字段:
数据如下所示:
store;ean;price;retail_price;quantity
001;0888721396226;200;200;2
001;0888721396233;200;159;2
001;2194384654084;299;259;7
001;2194384654091;199.95;199.95;8
如果“价格”等于“retail_price”,字段 retail_price 必须减少给定的百分比值,例如-10%
因此在示例数据中,第一行和最后一行应更改为 180 和 179,955
我是 pandas 的新手,在阅读“入门”部分后,我没有找到任何可以设置的内容...
所以任何帮助或提示(只要指出我的方向,然后我会 fiddle 自己解决)都很感激,
亲切的问候!
使用Series.eq
for compare both values and if same multiple retail_price
by 0.9
else not in numpy.where
:
mask = df['price'].eq(df['retail_price'])
df['retail_price'] = np.where(mask, df['retail_price'].mul(0.9), df['retail_price'])
print (df)
store ean price retail_price quantity
0 1 888721396226 200.00 180.000 2
1 1 888721396233 200.00 159.000 2
2 1 2194384654084 299.00 259.000 7
3 1 2194384654091 199.95 179.955 8
或者您可以使用 DataFrame.loc
用于多个仅匹配的行 0.9
:
mask = df['price'].eq(df['retail_price'])
df.loc[mask, 'retail_price'] *= 0.9
#working like
df.loc[mask, 'retail_price'] = df.loc[mask, 'retail_price'] * 0.9
编辑:对于不匹配掩码的过滤行(掩码中有错误)使用:
df2 = df[~mask].copy()
print (df2)
store ean price retail_price quantity
1 1 888721396233 200.0 159.0 2
2 1 2194384654084 299.0 259.0 7
print (mask)
0 True
1 False
2 False
3 True
dtype: bool
这是我的代码:
import pandas as pd
import numpy as np
import sys
with open('prozente.txt', 'r') as f: #create multiplicator from static value in File "prozente.txt"
prozente = int(f.readline())
mulvalue = 1-(prozente/100)
df = pd.read_csv('1.csv', sep=';', header=1, names=['store','ean','price','retail_price','quantity'])
mask = df['price'].eq(df['retail_price'])
df['retail_price'] = np.where(mask, df['retail_price'].mul(mulvalue).round(2), df['retail_price'])
df2 = df[~mask].copy()
df.to_csv('output.csv', columns=['store','ean','price','retail_price','quantity'],sep=';', index=False)
print(df)
print(df2)
将此用作 1.csv:
store;ean;price;retail_price;quantity
001;0888721396226;200;200;2
001;0888721396233;200;159;2
001;2194384654084;299;259;7
001;2194384654091;199.95;199.95;8
文件“prozente.txt”的内容是
25
我需要做的是比较 csv 文件中一行中的 2 个字段:
数据如下所示:
store;ean;price;retail_price;quantity
001;0888721396226;200;200;2
001;0888721396233;200;159;2
001;2194384654084;299;259;7
001;2194384654091;199.95;199.95;8
如果“价格”等于“retail_price”,字段 retail_price 必须减少给定的百分比值,例如-10%
因此在示例数据中,第一行和最后一行应更改为 180 和 179,955
我是 pandas 的新手,在阅读“入门”部分后,我没有找到任何可以设置的内容...
所以任何帮助或提示(只要指出我的方向,然后我会 fiddle 自己解决)都很感激,
亲切的问候!
使用Series.eq
for compare both values and if same multiple retail_price
by 0.9
else not in numpy.where
:
mask = df['price'].eq(df['retail_price'])
df['retail_price'] = np.where(mask, df['retail_price'].mul(0.9), df['retail_price'])
print (df)
store ean price retail_price quantity
0 1 888721396226 200.00 180.000 2
1 1 888721396233 200.00 159.000 2
2 1 2194384654084 299.00 259.000 7
3 1 2194384654091 199.95 179.955 8
或者您可以使用 DataFrame.loc
用于多个仅匹配的行 0.9
:
mask = df['price'].eq(df['retail_price'])
df.loc[mask, 'retail_price'] *= 0.9
#working like
df.loc[mask, 'retail_price'] = df.loc[mask, 'retail_price'] * 0.9
编辑:对于不匹配掩码的过滤行(掩码中有错误)使用:
df2 = df[~mask].copy()
print (df2)
store ean price retail_price quantity
1 1 888721396233 200.0 159.0 2
2 1 2194384654084 299.0 259.0 7
print (mask)
0 True
1 False
2 False
3 True
dtype: bool
这是我的代码:
import pandas as pd
import numpy as np
import sys
with open('prozente.txt', 'r') as f: #create multiplicator from static value in File "prozente.txt"
prozente = int(f.readline())
mulvalue = 1-(prozente/100)
df = pd.read_csv('1.csv', sep=';', header=1, names=['store','ean','price','retail_price','quantity'])
mask = df['price'].eq(df['retail_price'])
df['retail_price'] = np.where(mask, df['retail_price'].mul(mulvalue).round(2), df['retail_price'])
df2 = df[~mask].copy()
df.to_csv('output.csv', columns=['store','ean','price','retail_price','quantity'],sep=';', index=False)
print(df)
print(df2)
将此用作 1.csv:
store;ean;price;retail_price;quantity
001;0888721396226;200;200;2
001;0888721396233;200;159;2
001;2194384654084;299;259;7
001;2194384654091;199.95;199.95;8
文件“prozente.txt”的内容是
25